JAVA中基本类型Boolean占几个字节
今天在整理JAVA基礎知識時發(fā)現幾大基本數據類型的封裝類都有其BYTES值,也就是位數,除了Boolean。特意查了下資料,發(fā)現有幾大說法,如下:
1.單個的boolean 類型變量在編譯的時候是使用的int 類型。
boolean a=true;//這個a在JVM中占4個字節(jié)即:32位。
2.boolean 類型的數組時,在編譯的時候是作為byte array來編譯的所以boolean 數組里面的每一個元件占一個字節(jié),
boolean[] b = new boolean[10];//數組時,每一個boolean在JVM中占一個字節(jié)。理由:
1)JAVA規(guī)范中沒有定義boolean類型的大小。
2)但是:在JVM規(guī)范第2版中講得十分清楚。我上邊的結論就是從它當中取出來的。
根據:(JVM規(guī)范第2版 3.3.4節(jié))
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規(guī)范中,沒有明確指出boolean的大小。在《Java虛擬機規(guī)范》給出了4個字節(jié),和boolean數組1個字節(jié)的定義,具體還要看虛擬機實現是否按照規(guī)范來,所以1個字節(jié)、4個字節(jié)都是有可能的
總結
以上是生活随笔為你收集整理的JAVA中基本类型Boolean占几个字节的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL如何创建沙箱,沙箱环境搭建 -
- 下一篇: C语言进行数据指定步长的区间统计