The problem of bytes occupied by the basic boolean type of java

When reviewing the basic data types of java today, the numeric byte (-128-127) occupies one byte, the short occupies two bytes, the int occupies four bytes, the long occupies 8 bytes, and the floating-point float occupies 4 bytes, double occupies 8 bytes, character type char occupies 2 bytes, and boolean type boolaen occupies 1 bit, so how many bytes does the converted byte occupy?

I searched the Internet with questions, and found that someone had already given a reply:

1. 1 bit

The reason is that the value of boolean type has only two logical values, true and false, which will be represented by 1 and 0 after compilation. These two numbers only need 1 bit (bit) in memory to store, and bit is the smallest storage of the computer. unit.

2. 1 byte

The reason is that although 1 and 0 only need to occupy 1 bit of space after compilation, the minimum unit of computer processing data is 1 byte, and 1 byte is equal to 8 bits. The actual storage space is: use the lowest bit of 1 byte Store, the other 7 bits are filled with 0, if the value is true, the stored binary is: 0000 0001, if it is false, the stored binary is: 0000 0000.

3, 4 bytes

The source of the reason is the description in the book "Java Virtual Machine Specification": "Although the data type boolean is defined, it only provides very limited support. There are no bytes dedicated to boolean values ​​in the Java virtual machine. The code instruction, the boolean value operated by the Java language expression, is replaced by the int data type in the Java virtual machine after compilation, and the boolean array will be encoded into the byte array of the Java virtual machine, and each element boolean element occupies 8 bits". In this way, we can conclude that the boolean type occupies 4 bytes for individual use and 1 byte in the array.

Obviously the third item is a more accurate statement, so why does the virtual machine use int instead of boolean? Why not use byte or short, isn't it more memory-efficient. Most people will naturally think like this. I also have this question. After consulting the data, I found that the reason for using int is that for the current 32-bit processor (CPU), processing data at a time is 32 bits (here is not It refers to the 32/64-bit system, but refers to the CPU hardware level), which has the characteristics of efficient access.

Final summary:

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

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: The Boolean data type has only two possible values: true and false. Use this data type for a simple token that tracks true/false conditions. This data type represents this bit of information, but its "size" is not precisely defined.

It can be seen that the boolean type does not give a precise definition. The Java Virtual Machine Specification gives a definition of 4 bytes and a boolean array of 1 byte. It depends on whether the virtual machine implementation follows the specification, so 1 byte, 4 bytes are possible. This is actually a game between computing efficiency and storage space, both of which are very important.


From
Jianshu Dashen (Author: Ma Jianwei
Link : https://www.jianshu.com/p/2f663dc820d0
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.), The original link is: https://www.jianshu.com/p/2f663dc820d0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325019446&siteId=291194637