阅读 860

java中boolean类型占几个字节

blog.csdn.net/u010642004/…

java的基本数据类型中,boolean只有两种状态,默认值为false.取值范围是{true,false},理论上占1bit,实际上: 

1.单个的boolean 类型变量在编译的时候是使用的int 类型。 boolean a=true;//这个a在JVM中占4个字节即:32位。 

2.boolean 类型的数组时,在编译的时候是作为byte array来编译的所以boolean 数组里面的每一个元件占一个字节, 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.

 --------------------- 版权声明:本文为CSDN博主「zhangk64」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/u010642004/article/details/50099913