腾讯面试官问我Java中boolean类型占用多少个字节?我说一个,面试官让我回家等通知...
来自:程序员乔戈里
---------------第二天--------------
什么是boolean类型,根据官方文档的描述[1]:
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.
谷歌翻译一下:
布尔类型:布尔数据类型只有两个可能的值:真和假。使用此数据类型为跟踪真/假条件的简单标记。这种数据类型就表示这一点信息,但是它的“大小”并不是精确定义
*就有关于boolean占几个字节的讨论。 what-is-the-size-of-a-boolean-variable-in-java[2] 其中有一个高赞回答:
/**
*出自公众号:程序员乔戈里
*/
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();
}
}
运行结果
Size for LotsOfBooleans: 8257544
Average size: 82.57544
Size for LotsOfInts: 33599984
Average size: 335.99984
Java虚拟机规范一书提到 :
•在Java虚拟机中没有任何供 boolean值专用的字节码指令,Java语言表达式所操作的 boolean值,在编译之后都使用Java虚拟机中的int数据类型来代替。•Java虚拟机直接支持 boolean类型的数组,虚拟机的 navarra指令参见第6章的newarray小节可以创建这种数组。boolean类型数组的访问与修改共用byte类型数组的baload和 bastore指令。
•因为在虚拟机规范中说了,boolean值在编译之后都使用Java虚拟机中的int数据类型来代替,而int是4个字节,那么boolean值就是4个字节。•boolean类型数组的访问与修改共用byte类型数组的baload和 bastore指令,因为两者共用,只有两者字节一样才能通用呀,所以byte数组中一个byte是1个字节,那么boolean数组中boolean是1个字节。•总结:boolean在数组情况下为1个字节,单个boolean为4个字节。
Java规范中,没有明确指出boolean的大小。在《Java虚拟机规范》给出了单个boolean占4个字节,和boolean数组1个字节的定义,具体 还要看虚拟机实现是否按照规范来,所以1个字节、4个字节都是有可能的[3]
References
[1]
官方文档的描述: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html[2]
what-is-the-size-of-a-boolean-variable-in-java: https://*.com/questions/383551/what-is-the-size-of-a-boolean-variable-in-java[3]
所以1个字节、4个字节都是有可能的: https://blog.csdn.net/makingadream/article/details/53100237
------ END ---------
长按订阅更多精彩▼
如有收获,点个在看,诚挚感谢