贸易网站设计,如何自己创建app软件,衡阳网站建设设计,服务平台收件箱今天在整理JAVA基础知识时发现几大基本数据类型的封装类都有其BYTES值,也就是位数,除了Boolean。特意查了下资料,发现有几大说法,如下#xff1a;
1.单个的boolean 类型变量在编译的时候是使用的int 类型。 boolean atrue;//这个a在JVM中占4个字节即#xff1a;32位。
2.bo…今天在整理JAVA基础知识时发现几大基本数据类型的封装类都有其BYTES值,也就是位数,除了Boolean。特意查了下资料,发现有几大说法,如下
1.单个的boolean 类型变量在编译的时候是使用的int 类型。 boolean atrue;//这个a在JVM中占4个字节即32位。
2.boolean 类型的数组时在编译的时候是作为byte array来编译的所以boolean 数组里面的每一个元件占一个字节 boolean[] b new boolean[10];//数组时每一个boolean在JVM中占一个字节。理由
1JAVA规范中没有定义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.
还有说占一位的
3、1个bit 理由是boolean类型的值只有true和false两种逻辑值在编译后会使用1和0来表示这两个数在内存中只需要1位bit即可存储位是计算机最小的存储单位。
总结java规范中没有明确指出boolean的大小。在《Java虚拟机规范》给出了4个字节和boolean数组1个字节的定义具体还要看虚拟机实现是否按照规范来所以1个字节、4个字节都是有可能的