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:

1, 1 bit

The reason is that the Boolean value is only true and false two logical values, after compilation will use 1 and zero, these two numbers in memory only need 1 bits (bit) can be stored, the bit is the smallest storage unit of the computer.

2, 1 bytes

The reason is that although the compiled 1 and 0 only occupy 1-bit space, but the computer processing the smallest unit of data is 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.

3, 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.

Obviously the third is more accurate, why should the virtual machine 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 that for the current 32-bit processor (CPU), one processing data is 32 bits (this is not referring to the 32/64-bit system, but the CPU hardware level), With efficient access to the features.

The final summary:

According to the description of the official http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html document:

Boolean:the Boolean data type has only possible values:true and false. The use of this data type is simple, flags that track true/false conditions. This data type is 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.

As can be seen, the Boolean type does not give a precise definition, the Java Virtual Machine specification gives 4 bytes, and a Boolean array of 1 byte definitions, depending on whether the virtual machine implementation according to specifications, so 1 bytes, 4 bytes is possible. This is actually the game between computational efficiency and storage space, both of which are very important.

How many bytes a Boolean type occupies in Java

Related Article

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.