boolean占用几个字节

Although the Java virtual machine defines a boolean type, it only provides very limited support for it. There are no Java virtual machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java virtual machine int data type.

The Java virtual machine does directly support boolean arrays. Its newarray instruction enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore.2

The Java virtual machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java virtual machine type int, the compilers must use the same encoding.

csdn上的有人这样说

我的结论是:
1)boolean a=true;//这个a在JVM中占4个字节即:32位。
2)boolean[] b = new boolean[10];//数组时,每一个boolean在JVM中占一个字节。
理由:
1)JAVA规范中没有定义boolean类型的大小。
2)但是:在JVM规范第2版中讲得十分清楚。我上边的结论就是从它当中取出来的。
根据:(JVM规范第2版 3.3.4节)
Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java virtual machine int data type.
Where Java programming language boolean values are mapped by compilers to values of Java virtual machine type int, the compilers must use the same encoding. 而:Java virtual machine type int, whose values are 32-bit signed two's-complement integers。
Arrays of type boolean are accessed and modified using the byte array instructions
In Sun's JDK releases 1.0 and 1.1, and the Java 2 SDK, Standard Edition, v1.2, boolean arrays in the Java programming language are encoded as Java virtual machine byte arrays, using 8 bits per boolean element.


我比较赞同这一点

我一开始也认为是用int类型表示的 boolean
但boolean 数组的问题 学习下、、、、、
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值