Java, boolean type value in the end accounted for a few bytes of the problem?

Boolean type definition :

Boolean data type has only two possible values: true and false. This data type is used to track true / false condition simple tag. This data type to represent this information, but its "size" is not precisely defined.

In other words, java specification, did not specify the size of the boolean.

There are three kinds of argument:

1, 1 bit (1/8 bytes)
reasons: a boolean value of true and false of only two kinds of logical value 0 and 1 will be used in the compiled to represent, these two numbers bit count in memory, only one (bit) can be stored, the computer is bit smallest unit of storage. .


2,1 bytes
reason: While a compiled and 0 occupies only a space, but the minimum processing unit of the computer data is 1 byte, 1 byte is equal to 8, the actual storage space is: 1 bytes lowest bit memory, the other seven with 0 fill, if the value is true, then the stored binary as follows: 0000 0001, if it is false, then the stored binary is: 0000 0000.

3,4 bytes
reasons: In the description of the book "Java Virtual Machine Specification": "Although this boolean data type is defined, but it provides only a very limited support for no Java Virtual Machine. private boolean value bytecode instructions, boolean expressions in the Java language operation, use the data type int Java virtual machine after compilation instead and boolean array will be encoded byte array into a Java virtual machine, each element boolean element accounted for eight. " That JVM specification states that as a boolean int handle, which is 4 bytes, boolean array as a byte array processing, so that we can come alone accounted for boolean type is four bytes, one word is defined in the array section.

If you agree with the third, then there is a question: Why use that virtual machine instead of boolean int it? Why not byte or short, so do not save more memory space. Access to information through discovery, use int reason is that for the current 32-bit processor (CPU), the data processing time is 32 (this does not mean that the 32/64 bit systems, but to the CPU hardware level), 32-bit CPU uses 4 bytes is the most savings, even if you are a bit he is also 4 bytes. Since only 32-bit addressing system CPU 32-bit addressable having access efficiency characteristics.

4, It depends on Java virtual machine. This depends on the java virtual machine.

Summary: java specification does not specify the size of the boolean. In the "Java Virtual Machine Specification" gives four bytes, and defines an array of boolean byte, whether the implementation depends on the specific virtual machine to the specifications, it is a byte, four bytes are all possible.

Guess you like

Origin www.cnblogs.com/fyscn/p/11390538.html