Loading and storing of basic data types
Geek time in-depth understanding of Java Virtual Machine Review, errors and please correct me
The Boolean type in the virtual machine
In the Java language Specification, there are only two possible values for a Boolean type, which is "true" and "false". However, these two symbols cannot be used directly by the Java Virtual machine.
In the Java Virtual Machine specification, the Boolean type is mapped to the int type. That is, "true" is mapped to an integer 1, and "false" is mapped to an integer of 0. This coding rule constrains the implementation of Java bytecode.
Basic types in Java
There are 8 basic types in Java, the above Boolean type, the integer type Byte, short, char, int and long, and float type float and double.
Java's basic type has a corresponding value range and default values, the range from top to bottom increases once, the following range contains the previous value, meaning that the above data type conversion to the following data type does not need to be cast. Also note that the default values look different and are actually 0 in memory.
Of the 8 basic data types, only char and Boolean are unsigned numbers. and the value range of the Boolean type is 0 or the range of 1,char type is [0,65535]. We generally think that the value of the char type is non-negative.
The floating-point type of Java is in IEEE 754 floating-point number format. In the case of float, floating-point types typically have two 0,+0.0f and -0.0f. The former is 0 in Java, the latter is a floating-point number with a sign bit of 1 and the other bits are 0, which is equivalent to the hexadecimal integer 0x8000000 in memory (i.e. -0.0f can be obtained by float.intbitstofloat (0x8000000)). Although their memory
The values are different, but +0.0f = = -0.0f in Java returns True.
With +0.0f and -0.0f, the positive infinity and negative infinity in the floating-point number can be defined. Positive infinity is any positive floating point number (excluding +0.0f) divided by +0.0f, and negative infinity is the value of any positive floating-point number divided by -0.0f. In Java, positive infinity and negative infinity have exact values that are equivalent in memory to hexadecimal integers 0x7f800000 and 0xff800000.
What about the numbers that are out of range? The corresponding is Nan (not-a-number), 0x7fc00000 is the standard Nan, the other is called the non-standard Nan. And Nan has an attribute, any number! = NaN always returns TRUE.
Size of Java base type
Size when storing
Java virtual machines each call to a Java method creates a stack frame. Temporarily understand the explanatory stack frame used for the interpreter. Consists of two components, a local variable area, and a byte-code operand stack.
In the Java Virtual Machine specification, the local variable area is equivalent to an array and can be indexed with a positive integer. In addition to long and double values that need to be stored with two array cells, the other base types and values of reference types occupy an array cell.
That is, the four types of Boolean, Byte, Char, short, occupy space on the stack and int is the same, and the reference type is the same. The 32-bit hotspot, which occupies 4 bytes on the stack and 64-bit hotspot, takes up 8 bytes.
This, of course, exists only in local variables, not on fields and arrays of elements stored on the heap. For the three types of fields or arrays of Byte, char, and short, the space they occupy on the heap is one byte, two bytes, and two bytes, which is consistent with the range.
Therefore, when we store a value of type int to a field or array of these types, it is equivalent to doing an implicit mask operation. For example, when we store 0xFFFFFFFF (-1) in a field declared as a char type, the
This field only accounts for two bytes, so the high two bits of bytes are intercepted and eventually deposited in "\UFFFF".
The Boolean field and the Boolean array are more special. In the HotSpot, the Boolean field occupies one byte, and the Boolean array is implemented directly with a byte array. To ensure that the Boolean value in the heap is legal, the HotSpot explicitly masks operations when it is stored, that is, only the last value is saved in a Boolean field or array.
Size at load time
The arithmetic operations of a Java virtual machine depend almost entirely on the operand stack. That is, we need to load the Boolean, Byte, Char, and short in the heap onto the operand stack and then operate the value on the stack as an int type.
For the two unsigned types of Boolean, char, the load is accompanied by a 0 extension. For example, a char has a size of two bytes. At load time, the value of char is copied to the lower two bytes of the int type, and the sophomore bytes are filled with zeros.
For both types of byte and short, loading is accompanied by a symbol extension. For example, the size of a short is two bytes. The value of short at load time is also copied to the lower two bytes of the int type. If the short value is a non-negative number, the most
The high position is 0, then the sophomore byte of the value of the int type is filled with zero, otherwise it is populated with a.
Summarize
Where the Boolean type is mapped to an integer type in the Java virtual machine: "True" is mapped to 1, and "false" is mapped to 0. Logical operations in Java code, and conditional jumps, are implemented using integer-related byte codes.
In addition to the Boolean type, Java has 7 additional basic types. They have different ranges, but the default values are 0 in memory. Among these basic types, floating-point types are more special. Based on its operations or comparisons, the +0.0f, -0.0f, and NaN conditions need to be considered.
In addition to long and double, the other base types are consistent with the size of the reference type in the method stack frames that interpret the execution, but they occupy a different size in the heap. The values of Boolean, Byte, char, and short are stored in the field or
The Java virtual opportunity masks operations when the array unit is When read, the Java virtual machine expands it to the int type.
I understand.
From this, you can learn that the size of the 8 basic data, and what the virtual machine is for the basic data type, the size of the byte, short, char, and Boolean storage on the stack frame on the heap and the interpretation execution is not the same, the operation is masked when loading (reading).
Basic types of Java