boolean類型長度

 

單個布爾類型變量使用 int 值來表示,布爾數組採用 byte 數組來表示。 

true 使用 int 常量 1 表示,false 使用 int 常量 0 表示。 而int在java中是4個字節表示。
java

建議查看java API官方文檔,裏面有詳細說明:

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
相關文章
相關標籤/搜索