黑马程序员技术交流社区

标题: java中的 boolean 在内存中占多少字节? [打印本页]

作者: fyg408224345    时间: 2015-6-7 16:48
标题: java中的 boolean 在内存中占多少字节?
以前看视频讲的java中boolean占1位,即1/8个字节。今天上课老师好像说boolean占1个字节。到底是什么啊?
作者: 一休    时间: 2015-6-7 20:19
老师说业界默认一个字节
作者: TOBESKY    时间: 2015-6-7 20:24
一楼正解,内存中基本的存储单位是字节,一个字节8个bit.
作者: Enhon1992    时间: 2015-6-7 20:46
一个byte
作者: gainn    时间: 2015-6-7 20:47
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字节。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2