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.
PS(请注意最后几句):
sun's Data Types introduction: byte: The byte data type is an 8-bit signed two's complement integer short: The short data type is a 16-bit signed two's complement integer int: The int data type is a 32-bit signed two's complement integer long: The long data type is a 64-bit signed two's complement integer float: The float data type is a single-precision 32-bit IEEE 754 floating point double: The double data type is a double-precision 64-bit IEEE 754 floating point. char: The char data type is a single 16-bit Unicode character boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its size isn't something that's precisely defined.
假设计算机显示器的颜色质量为256色,256=2的8次方,所以256色确实是一个象素一字节。 从定义上来看,像素是指基本原色素及其灰度的基本编码。像素是构成数码影像的基本单元,通常以像素每英寸PPI(pixels per inch)为单位来表示影像分辨率的大小。如果用1字节表示一个像素最多可以有256种颜色。其中,一个字节是8位。一个像素如果是单色表示,那么每个像素用1个二进制位存储。如果一个像素有4种颜色,那么就需要2个二进制位存储。