What are the primitive data types in Java, what are their sizes and corresponding encapsulation classes?
(1)boolean
The boolean data type is either true or false . This data type represents 1 bit of information, but its size is not precisely defined.
The " Java Virtual Machine Specification" says: " Although the boolean data type is defined, it only provides very limited support. There are no bytecode instructions for boolean values in the Java virtual machine. The Java language The boolean value manipulated by the 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 is 4 bytes alone and 1 byte in the array . Why does the virtual machine use int instead of boolean ? why not use byte or short, doesn't this save more memory space? In fact, the reason for using int is that it is more efficient to exchange 32 bits of data at a time for today's 32 -bit CPUs .
To sum up, we can know that the official document does not give a precise definition of the boolean type. The "Java Virtual Machine Specification" gives the definition of "use 4 bytes when alone, and 1 byte when boolean array". It depends on whether the virtual machine implementation is in accordance with the specification, so 1 byte and 4 bytes are possible. This is actually a space-time tradeoff.
The wrapper class for boolean type is Boolean.
(2 )bytes —— 1 byte —— Byte
(3)short——2 bytes——Short
(4)int——4 bytes——Integer
(5)long——8 bytes——Long
(6)float——4 bytes——Float
(7)double——8 bytes——Double
(8)char——2 bytes——Character