true 使用 int 常量 1 表示,false 使用 int 常量 0 表示。 而int在java中是4个字节表示。
建议查看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
本文转自 zhouhaipeng 51CTO博客,原文链接:http://blog.51cto.com/tianya23/718299,如需转载请自行联系原作者