Summary of java interview knowledge points 1

 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

3short——2 bytes——Short

4int——4 bytes——Integer

5long——8 bytes——Long

6float——4 bytes——Float

7double——8 bytes——Double

8char——2 bytes——Character

 

Guess you like

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