A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

以前看视频讲的java中boolean占1位,即1/8个字节。今天上课老师好像说boolean占1个字节。到底是什么啊?

4 个回复

倒序浏览
老师说业界默认一个字节
回复 使用道具 举报
一楼正解,内存中基本的存储单位是字节,一个字节8个bit.
回复 使用道具 举报
一个byte
回复 使用道具 举报
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 (§newarray) enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore (§baload, §bastore).

In Oracle’s Java Virtual Machine implementation, boolean arrays in the Java programming language are encoded as Java Virtual Machine byte arrays, using 8 bits per boolean element.

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.

--------------------------------------------------------------
以上节选自Java SE 7版本的JVM说明档2.3.4节。
大意就是boolean作为数组操作时只占1个字节,作为单个时会转成int型占4字节。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马