See a friend in the group ask, think of before also met this problem, also do not have a clear answer, so he went online to find the following answer.
The conclusion is as follows:
-
boolean type is compiled into int type for use, accounting for 4 byte s.
-
The boolean array is compiled into byte array type, and each boolean array member accounts for 1 byte.
-
In Java virtual machine, 1 means true, 0 means false.
-
This is just a suggestion for Java virtual machines.
-
To be sure, it won't be one bit.
From https://binkery.com/archives/346.html
How much space does the boolean type take up in Java? Just now, I saw it on CSDN. In an old post, some people talked about it. Some people said 1 bit, some said 1 byte, some said 4 bytes. How much is that?
Let's start with an official document.
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.
It's clear that boolean only has two values: true and false. This data type only represents 1 bit of information, but its "size" is not strictly defined. In other words, no matter how much space it occupies, only one bit of information is meaningful.
Let's see how the Java virtual machine specification is written.
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.
Although the Java virtual machine defines the boolean type, its support is limited and there is no special virtual machine instruction. In the Java language, operations on Boolean values are replaced with int data types.
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 virtual machine does not directly support boolean array. boolean type array and byte array common instructions.
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.
In the implementation of Oracle's Java virtual machine, the boolean array in Java language is encoded as byte array of Java virtual machine, each element occupies 8 bits.
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 virtual machine uses 1 for true and 0 for false to encode boolean array. When the boolean value of the Java language is mapped to the int type of the Java virtual machine by the compiler, the same encoding is used.
Ok, now we can summarize.
boolean type is compiled into int type for use, accounting for 4 byte s. The boolean array is compiled into byte array type, and each boolean array member accounts for 1 byte. In Java virtual machine, 1 means true, 0 means false. This is just a suggestion for Java virtual machines. To be sure, it won't be one bit.
- EOF -