How many bytes does the boolean type in Java occupy?

introduction

	      ⾸先在Java中定义的基本数据类型中,四类八种, 除了其它七种类型都有明确的内存占⽤字节数外,
	只有 boolean 类型没有给出具体的占⽤字节数,因为对虚拟机来说根本就不存在 boolean 这个类型,
	boolean类型在编译后会使⽤其他数据类型来表示,那boolean类型究竟占⽤多少个字节?
	
	     带着疑问,随便⽹上⼀搜,答案五花⼋⻔,基本有以下⼏种:

Boolean Type Definition : The Boolean data type has only two possible values: True and False.
Use this data type for simple tokens that track true/false conditions.
This data type represents this bit of information, but its "size" is not precisely defined.
In other words, in the Java specification, the size of boolean is not clearly indicated.

Reply from Chat AI

In Java, the memory space occupied by the Boolean type (that is, the boolean type) is 1 byte (8 bits), and it has only two values: true and false. This is because in Java, a byte is the smallest addressable memory unit. In addition, in order to facilitate data processing and save memory, Java's Boolean type only needs to occupy one byte to meet the demand;

In Java, the memory space occupied by the Boolean type is 1 byte (8 bits), not 1 bit or 4 bytes. This is because the Java virtual machine specification clearly stipulates that the size of the Boolean type is 1 byte (8 bits). Although theoretically 1-bit storage can be used to represent the Boolean type, the Java virtual machine specification stipulates that 8 bits are the smallest addressable memory unit. In order to comply with the specification, the Java Boolean type uses 1-byte storage.

For Boolean arrays, multiple Boolean values ​​will be stored together consecutively, but the Java virtual machine requires that the size of each array element must be 8 bits, so each element in a Boolean array occupies 1 byte. space.

Statement 1: 1 bit

The reason is that the value of type boolean 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) to be stored in the memory, and the bit is the minimum value of a computer. storage unit.

Statement 2: 1 byte

Although 1 and 0 only need to occupy 1 bit of space after compilation, the smallest unit of computer processing data is 1 byte, and 1 byte is equal to 8 bits. The actual storage space is: stored with the lowest bit of 1 byte, The other 7 bits are filled with 0. If the value is true, the stored binary value is: 0000 0001, and if it is false, the stored binary value is: 0000 0000.
It is consistent with what CHAT AI said;

Statement 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 for it. There is no special boolean value in the Java virtual machine. The bytecode instructions used and the boolean value operated by the Java language expression are replaced by the int data type in the Java virtual machine after compilation, and the boolean array will be encoded into a byte array of the Java virtual machine. Each element boolean element occupies 8 bits".
In this way, we can conclude that the boolean type occupies 4 bytes when used alone, and 1 byte in the array.

Obviously the third statement is more accurate, so why does the virtual machine use int instead of boolean?
Why not use byte or short, which saves more memory space?
Most people will naturally think this way, and I also have this question. After consulting the data, I found that the reason for using int is:

Most of the current processors are 32-bit (not the 32-bit operating system, but the CPU hardware), storing boolean data into 4 bytes (32-bit), the access efficiency is the highest.

summary:

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

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 as a simple token to track 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 the definition of 4 bytes and 1 byte of the boolean array. 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.

Guess you like

Origin blog.csdn.net/Kaka_csdn14/article/details/130833524