我一直在尝试让一些流通过 TCP 工作,但似乎失败了,可能是因为我对字节流了解不够。
我知道它是一个字节,它是 8 位。示例:0000 0001(即“int 1”)
当我定义时,我们说:
Byte[] myByte = new byte[1];
“1”有什么作用? myByte 只能承载一个字节吗?
最佳答案
new type[x]
是创建数组 [object] 的表达式语法(类型为 type[]
1) 与 x
元素。
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array [object] is created (i.e.
new byte[1]
). After creation, its length is fixed ..
因此,new byte[1]
为单个字节(长度 = 1)创建一个数组 [object],而 new byte[1024]
创建一个数组 [object] 1024 字节元素(长度 = 1024)。
1 帖子中的代码有点“有趣”,因为它使用 Byte[]
作为数组类型,但使用 new byte[1]
创建实际的数组对象;两个地方都应该是byte
。我忽略了这个错误,因为原始数组的自动装箱是 not supported在Java中。
关于Java:字节[1]与字节[1024],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22445022/