java boolean 大小_java boolean 大小

先看官方文档

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases.

...

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类型的大小。

这在stackoverflow中的这个问题有所提及

这里讲到

It's virtual machine dependent. // 由虚拟机自己实现

而我在其他地方查到了如下资料:

根据:(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.

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

单个的boolean 类型变量在编译的时候是使用的int 类型。

boolean b = true; // 这个b在JVM中占4个字节即:32位

boolean 类型的数组时,在编译的时候是作为byte array来编译的所以boolean 数组里面的每一个元素占一个字节(8位)

boolean[] b = new boolean[10]; // 数组时,每一个boolean在JVM中占一个字节。

验证

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();

}

}

/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=54327:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/apple/code/learning-demo/demo/out/production/untitled:/Users/apple/code/learning-demo/demo/lib/kotlin-stdlib.jar:/Users/apple/code/learning-demo/demo/lib/kotlin-reflect.jar:/Users/apple/code/learning-demo/demo/lib/kotlin-test.jar:/Users/apple/code/learning-demo/demo/lib/kotlin-stdlib-jdk7.jar:/Users/apple/code/learning-demo/demo/lib/kotlin-stdlib-jdk8.jar bigdata.Test

Size for LotsOfBooleans: 9483568

Average size: 94.83568

Size for LotsOfInts: 33599832

Average size: 335.99832

0

Process finished with exit code 0

测试结果

openjdk 11.0.2

boolean 32位(int)

boolean[] 8位(byte)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值