面试官:Java中boolean类型占多少字节?大多数人都回答错了...

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达

上一篇:这300G的Java资料是我师傅当年给我的,免费分享给大家(已修复)

下一篇:昨天分享资料不小心把百度网盘深处的秘密泄露了已修复

作者 :Binkery

原文:https://binkery.com/archives/346.html

Java,数据类型,虚拟机,boolean

在 Java 中,boolean 类型占用多少空间呢?刚才在 CSDN 上看了一下,一个很老的帖子里有人讨论来着,有人说 1 bit ,有人说 1 byte , 也有人说 4 bytes ,那到底是多少呢?

先来个官方文档的说法。

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 值只有 true 和 false 两种,这个数据类型只代表 1 bit 的信息,但是它的“大小”没有严格的定义。也就是说,不管它占多大的空间,只有一个bit的信息是有意义的。

咱们再去看看 Java 虚拟机规范上是怎么写的。

Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. 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.

Java 虚拟机虽然定义了 boolean 类型,但是支持是有限的,没有专门的虚拟机指令。在 Java 语言中,对 boolean 值的操作被替换成 int 数据类型。

The Java Virtual Machine does directly support boolean arrays. Its newarray instruction (§newarray) enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore (§baload, §bastore).

Java 虚拟机没有直接支持 boolean 数组。boolean 类型数组和 byte 数组公用指令。

In Oracle’s Java Virtual Machine implementation, boolean arrays in the Java programming language are encoded as Java Virtual Machine byte arrays, using 8 bits per boolean element.

在 Oracle 的 Java 虚拟机实现中,Java 语言中的 boolean 数组被编码成 Java 虚拟机的 byte 数组,每个元素占 8 比特。

The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false . 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 虚拟机使用 1 表示 true ,0 表示 false 来编码 boolean 数组。Java 语言的 boolean 值被编译器映射成 Java 虚拟机的 int 类型的时候,也是采用一样的编码。

其实也就是stackoverflow上面那个老外的回答:依赖于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 = 1000000;

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

使用的jdk版本:JDK build 1.6.0_11,运行结果如下:

Size for LotsOfBooleans: 87978576
Average size: 87.978576
Size for LotsOfInts: 328000000
Average size: 328.0

你也可以在你当前的jdk版本运行一下,看看是什么结果。

Ok,现在可以总结一下了。

1.boolean 类型被编译成 int 类型来使用,占 4 个 byte 。

2.boolean 数组被编译成 byte 数组类型,每个 boolean 数组成员占 1 个 byte 

3.在 Java 虚拟机里,1 表示 true ,0 表示 false 。

4.这只是 Java 虚拟机的建议。

5.可以肯定的是,不会是 1 个 bit 。

说句题外话,springboot全家桶技术交流群可以加我微信,但是坑位有限哦,由于忙于工作,有时不能及时回复大家,请多包涵。


精彩推荐

1、100多个免费API接口分享调用完全不限次数,以后总用得着2、Java8中一个极其强悍的新特性,很多人没用过(非常实用)3、Spring Boot 的接口限流算法优缺点深度分析4、爱奇艺的 "数据库" 选型到底有多牛逼?5、JVM 性能调优监控工具 jps、jstack、jmap、jhat、jstat、hprof 使用详解6、未来有望干掉 Python 和 JavaScript 的编程语言7、冒着被开除风险也要给大家看看看这份Spring Cloud 总结8、全面了解 Nginx 主要应用场景

点个在看少个 bug

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值