java中boolean类型的大小

在Java中,int,float,double等基本类型所占字节大小都有明确说明,唯独boolean类型没有,那么boolean类型的大小究竟是多少呢?

Java SE Specification(java 8)

先看java官方规范中怎么说,规范网址,下面是摘抄:

The boolean Type

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定义了boolean类型,但并没有提供更多的支持,也没有任何一种jvm有专用于boolean的虚拟机指令,事实上,java程序中的boolean都被编译成int类型,也就是说,单个的boolean类型在编译后占4个字节。
jvm并不直接支持boolean类型的数组,boolean类型的数组其实是byte类型的数组,也就是说,boolean类型的数组编译后每个boolean元素是一个byte,也就是1个字节。
boolen数组中使用1表示true,0表示false,需要注意的是,虽然在数组中使用byte表示boolena,但是一旦将其取出,那么走出数组的boolean必须转换成int类型,这在boolena数组的baload, bastore中有所体现。

#### 操作码baload
The arrayref must be of type reference and must refer to an array whose components are of type byte or of type boolean. The index must be of type int. Both arrayref and index are popped from the operand stack. The byte value in the component of the array at index is retrieved, sign-extended to an int value, and pushed onto the top of the operand stack.  
将数组的某一byte元素取出转为int类型并push到栈上

操作码bastore

The arrayref must be of type reference and must refer to an array whose components are of type byte or of type boolean. The index and the value must both be of type int. The arrayref, index, and value are popped from the operand stack. The int value is truncated to a byte and stored as the component of the array indexed by index.
将int类型数据截断为byte类型,并存储在在数组内

为什么这么设计

the JVM uses a 32-bit stack cell, used to hold local variables, method arguments, and expression values. Primitives that are smaller than 1 cell are padded out, primitives larger than 32 bits (long and double) take 2 cells. This technique minimizes the number of opcodes, but does have some peculiar side-effects (such as the need to mask bytes).

Primitives stored in arrays may use less than 32 bits, and there are different opcodes to load and store primitive values from an array. Boolean and byte values both use the baload and bastore opcodes, which implies that boolean arrays take 1 byte per element.

opcodes就是操作码,也叫机内码,指令序列等,用来告诉CPU需要执行哪一条指令,上面的bastore就是操作码