How many bytes a Boolean type occupies in Java

Source: Internet
Author: User

Why ask this question, in the first eight basic data types defined in Java, in addition to the other seven types have a clear memory consumption of bytes, the Boolean type does not give a specific number of bytes occupied, because there is no Boolean type for the virtual machine, When a Boolean type is compiled, it is represented by a different data type, how many bytes does the Boolean type occupy? With questions, random online search, the answers are various, basically the following:

The 1 bit justification is that Boolean values are only true and false two logical values, which are compiled with 1 and zero, which are stored in memory with only 1 bits (bit), and bits are the smallest storage unit of the computer.

1 bytes The reason is that although the compiled 1 and 0 only occupy 1 bits of space, but the computer processing data is the smallest unit of 1 bytes, 1 bytes equals 8 bits, the actual storage space is: with 1 bytes of the lowest bit storage, the other 7 bits are filled with 0, if the value is true then the stored binary is: 0000 0001, if False, the stored binary is: 0000 0000.

4 bytes The source is the description in the Java Virtual Machine specification: "Although the data type of Boolean is defined, it provides very limited support." In a Java virtual machine, there are no bytecode directives dedicated to Boolean values, and the Boolean values that are manipulated by the Java language expression are replaced with the int data type from the Java Virtual machine after compilation. The Boolean array is encoded as a byte array of the Java virtual machine, with each element having a Boolean element of 8 bits. In this way we can conclude that the Boolean type takes up a single use of 4 bytes and is 1 bytes in the array.

The analysis is clearly the third one is more accurate, why the virtual machine to use int instead of Boolean? Why not use byte or short, which is not more memory-saving space. Most people will naturally think like this, I also have this question, after consulting the data found that the reason for using int is thatfor the current 32-bit processor (CPU), a single processing data is 32 bits (this does not refer to the 32/64-bit system, but the CPU hardware level), with the characteristics of efficient access。

Summary according toOfficial DocumentsDescription in http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html (no FQ required):
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 Type: Boolean data type has only two possible values: True and False. Use this data type as a simple token for tracking true/false conditions. This data type represents this information, but its "size" is not precisely defined . Can be seen,The Boolean type does not give an exact definition, the Java Virtual Machine specification gives a definition of 4 bytes, and a boolean array of 1 bytes, depending on whether the virtual machine implementation follows the specification, so 1 bytes, 4 bytes are possible. This is actually the game between computational efficiency and storage space, both of which are very important.

From for notes (Wiz)

How many bytes a Boolean type occupies in Java

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.