Java中boolean占的字节数

来源:互联网 发布:mac可以装bilibili吗 编辑:程序博客网 时间:2024/06/02 19:03

java中的8种基本数据类型基本都有明确的空间占用大小,但是对于boolean类型,并没有给出一个具体的大小。抱着疑惑在网上找了会,但是大家给出的说法有好几种,都不尽相同,有说占1bit,有1bite甚至是占4bites的。都有自己的道理,但是为什么会有这么些种情况也不是很了解。 直到看见了这个博客才弄明白了。一下便是他博客中些的内容,地址是

http://www.binkery.com/archives/346.html

在java官方文档中介绍如下:
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 类型的时候,也是采用一样的编码。

故可以总结出:

1. boolean 类型被编译成 int 类型来使用,占 4 个 byte 。
2. boolean 数组被编译成 byte 数组类型,每个 boolean 数组成员占 1 个 byte 。
3. 在 Java 虚拟机里,1 表示 true ,0 表示 false 。
4. 可以肯定的是,不会是 1 个 bit 。
5. boolean只是包含了一个bit大小的信息,在不同情况下实际转化成的大小不一样。

0 0
原创粉丝点击