騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun


騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

什麼是boolean類型,根據http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html:

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.

谷歌翻譯一下:

布爾類型:布爾數據類型只有兩個可能的值:真和假。使用此數據類型為跟蹤真/假條件的簡單標記。這種數據類型就表示這一點信息,但是它的“大小”並不是精確定義

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

stackoverflow就有關於boolean佔幾個字節的討論。https://stackoverflow.com/questions/383551/what-is-the-size-of-a-boolean-variable-in-java其中有一個高贊回答:

<code> /**
*出自公眾號:程序員喬戈裡
*/
class LotsOfBooleans
{
boolean a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af;
boolean b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf;
boolean c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf;
boolean d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df;
boolean e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef;
}

class LotsOfInts
{
int a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aa, ab, ac, ad, ae, af;
int b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf;
int c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, ca, cb, cc, cd, ce, cf;
int d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, da, db, dc, dd, de, df;
int e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, ea, eb, ec, ed, ee, ef;
}


public class Test
{
private static final int SIZE = 100000;

public static void main(String[] args) throws Exception
{
LotsOfBooleans[] first = new LotsOfBooleans[SIZE];
LotsOfInts[] second = new LotsOfInts[SIZE];

System.gc();
long startMem = getMemory();

for (int i=0; i < SIZE; i++)
{
first[i] = new LotsOfBooleans();
}

System.gc();
long endMem = getMemory();

System.out.println ("Size for LotsOfBooleans: " + (endMem-startMem));

System.out.println ("Average size: " + ((endMem-startMem) / ((double)SIZE)));

System.gc();
startMem = getMemory();
for (int i=0; i < SIZE; i++)
{
second[i] = new LotsOfInts();
}
System.gc();
endMem = getMemory();

System.out.println ("Size for LotsOfInts: " + (endMem-startMem));
System.out.println ("Average size: " + ((endMem-startMem) / ((double)SIZE)));

// Make sure nothing gets collected
long total = 0;
for (int i=0; i < SIZE; i++)
{
total += (first[i].a0 ? 1 : 0) + second[i].a0;
}
System.out.println(total);
}

private static long getMemory()
{
Runtime runtime = Runtime.getRuntime();
return runtime.totalMemory() - runtime.freeMemory();
}
}/<code>

運行結果

<code>Size for LotsOfBooleans: 8257544
Average size: 82.57544
Size for LotsOfInts: 33599984
Average size: 335.99984/<code>
騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

Java虛擬機規範一書提到 :

  • 在Java虛擬機中沒有任何供 boolean值專用的字節碼指令,Java語言表達式所操作的
    boolean值,在編譯之後都使用Java虛擬機中的int數據類型來代替。
  • Java虛擬機直接支持 boolean類型的數組,虛擬機的 navarra指令參見第6章的newarray小節可以創建這種數組。
    boolean類型數組的訪問與修改共用byte類型數組的baload和 bastore指令
騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

  • 因為在虛擬機規範中說了,boolean值在編譯之後都使用Java虛擬機中的int數據類型來代替,而int是4個字節,那麼boolean值就是4個字節。
  • boolean類型數組的訪問與修改共用byte類型數組的baload和 bastore指令
    ,因為兩者共用,只有兩者字節一樣才能通用呀,所以byte數組中一個byte是1個字節,那麼boolean數組中boolean是1個字節。
  • 總結:boolean在數組情況下為1個字節,單個boolean為4個字節。
騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

Java規範中,沒有明確指出boolean的大小。在《Java虛擬機規範》給出了單個boolean佔4個字節,和boolean數組1個字節的定義,具體 還要看虛擬機實現是否按照規範來,https://blog.csdn.net/makingadream/article/details/53100237

騰訊問我Java中boolean類型佔幾個字節?我說一個,面試官讓我gun

References:[1] 官方文檔的描述: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[2] what-is-the-size-of-a-boolean-variable-in-java: https://stackoverflow.com/questions/383551/what-is-the-size-of-a-boolean-variable-in-java[3] 所以1個字節、4個字節都是有可能的: https://blog.csdn.net/makingadream/article/details/53100237


分享到:


相關文章: