The length of a Boolean type in Java

Source: Internet
Author: User

In the recent summary, found that the size of the Boolean type is very tangled, and then looked at the Internet, and found that the views of the Internet are also different opinions.

With a question I downloaded the latest "The Java Virtual Machine specification" from the official website of Oracle, this is the seventh edition, the final revision date is 2012-7-27

This is explained in the JVM:

Although the Java Virtual machine defines a Boolean type,it only provides very limited for it.  There are no Java Virtual machine instructions solely dedicated to operations on Boolean values. Instead, expressions in the Java programming language this operate on Boolean values are compiled to use values of the JAV A virtual machine int data type.

The Java virtual machine does directly support Boolean arrays. Its newarray instruction enables creation of Boolean arrays. Arrays of type Boolean are accessed and modified using the byte array instruction baload and Bastore.

In Oracle 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.

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 CO Mpilers must use the same encoding.

One conclusion from the above explanation is that a single Boolean type variable is the type of int that is used at compile time. In the case of a Boolean array, each component in a Boolean array is compiled as a byte array at compile time, which is OK.

Personally think: A single Boolean type variable, Boolean B, which says B is four bytes like int, I don't think it's appropriate. When a Boolean type is compiled in the description above, the Boolean value is mapped to the compiler as an int, and there is no specific boolean-type instruction in the JVM. It should be that when a program reads a Boolean, it creates a space in the "data" area of the memory to hold true, or false.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.