Java:字节[1]与字节[1024]

标签 java byte

我一直在尝试让一些流通过 TCP 工作,但似乎失败了,可能是因为我对字节流了解不够。

我知道它是一个字节,它是 8 位。示例:0000 0001(即“int 1”)

当我定义时,我们说:

Byte[] myByte = new byte[1];

“1”有什么作用? myByte 只能承载一个字节吗?

最佳答案

new type[x] 是创建数组 [object] 的表达式语法(类型为 type[] 1) 与 x 元素。

请参阅Nuts & Bolts: Arrays

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/

相关文章:

java - HIPI API : does it process 1 image per map task?

Java 移位字节返回意外结果

java - 关于 Java 中线程的说明

java - 正确使用switch语句

c# - C# 中的非托管 dll 函数字节 * 参数返回

.net - 确保数字在 .NET 中正确转换

c# - 如何将此字节声明从 Java 转换为 C#?

python - 非常奇怪的Python 3字节到字符串失败

Java - 迭代器 : "Syntax error, parameterized types are only available if source level is 5.0"

java - 如何用正则表达式实现这个条件?