Chapter 11 type conversion

11.1 Conversion opcodes
Java virtual machine includes a number of basic types of operation code conversion work, no operands back operation codes of the switching operation performed, the converted value is obtained from the top of Zhan. Java virtual machine a value popped from the top of the stack, it transforms it, and then the conversion result onto the stack. For int, conversion between long, float and double types of operation code as shown in Table 11-1, for each type of the possible transitions between the four types, there is a corresponding operation code.

As shown in Table 11-2 operation code is converted to an int data type occupies less space than an int type. These opcodes are popped from the operand find an int type value, it can be converted to an int value byte, short, or char described, then, this value of type int person converted stack pressure. i2b pop instruction is truncated byte int type value type, then its sign-extended to restore an int. i2s pop instruction int type value is truncated short type, then its sign-extended to restore an int. i2c int pop instruction type Char truncated type value, and then restored to its zero-extended int.

The Java virtual machine is not long, float, double type values ​​directly into the data type occupies less space than the opcode type int. Therefore, the float type value into a byte requires two steps: first, the value must be converted into float type int type values ​​f2i command, then, the resulting values ​​then converted to an int type values ​​i2b byte instruction.

Despite the operation code can be converted to an int value than the value of type int takes up less space data type (byte, short, and char), but there is no operation code conversion operation performed in the opposite direction. Because any byte, short, and char in the press-fitting type values ​​for it, effectively has been converted into a value of type int. Accepting an instruction byte, short, and char type or the values ​​from the array of objects in the heap, and these values ​​will be pressed into the find command to convert them to an int type value. These instructions will be described in Chapter 15.

Relates byte, short, and char type will first arithmetic operation values are converted to an int type, int type value and then calculates, to obtain the final result of type int. Thus, if the two types of byte values are added, the final result will be a type of int. If the result needs to be a byte must be the result of an explicit type int to a byte type. For example, the following code causes the compiler to fail:
// at The CD-ROM in the On File with opcodes / EX1 / BadArithmetic.java
class BadArithmetic {

addOneAndOne byte static () {
byte = A. 1;
byte = B. 1;
byte C = A + B;
return C;
}
}
The operation can compile javac and generates GoodArithmetic.class file. This file contains the following addOneAndOne () method bytecode sequence:
0 iconst_1 // Constant 1. Push int
. 1 istore_0 // POP INTO local variable 0, A Which IS:
// A = byte. 1;
2 // iconst_1 . Again Push int. 1 Constant
. 3 istore_1 // POP INTO local variable. 1, B Which IS:
// byte = B. 1;
. 4 iload_0 // Push A (A IS AS AN already Stored in int
// local
// variable 0) .
. 5 iload_1 Push // B (B IS AS AN already Stored in int
// local
// variable. 1).
. 6 addition.Top of the Perform the iadd // Stack IS
// now (A + B), AN int.
// Result I2b the Convert int. 7 to byte (Result Still
// Occupies 32 bits).
. 8 istore_2 // Pop INTO local variable. 3, Which IS
// byte C: C = byte (byte) (A + B);
. 9 iload_2 the value of the Push C // SO IT CAN BE
// returned.
10 ireturn // Addition: return C;
that the convert () method will demonstrate the value of type int converts Java virtual machine byte type value manner. imInt variable start value 125, after each cycle, it will be incremented by one, and to a byte type. It then becomes a product of the original value and the value of -1, and finally again converted to byte type. This simulation process can quickly show what happens at the boundary of the effective range of a byte.

The maximum value of type byte 127, the minimum value is -128. Int type value within this range is directly converted into a byte value, when the int type values ​​outside of this valid range, things get interesting.

Java Virtual Machine extended by a truncated manner and signed converted into a byte value of an int value. The most significant bit ( "sign bit") long, int, short and pointed out that this type of byte int type value is positive or negative. If the sign bit is 0, the value of n; If the sign bit is 1, it is negative. Type bit 7 byte is the sign bit. Int type value to transition from a byte value, the value of 7 will be copied to the 8th to 31st bit. This produces an int type value, the original value of this value is converted to an int type byte result value obtained have the same value. After performing the truncation operation and the sign-extended, int type variables that will receive a valid value of type byte.

applet lists simulated what happened when converting a byte beyond the effective range of an int type value type byte. After e.g., imlnt variable is 128 (0x00000080), it is converted to a byte, byte type value obtained -128 (Oxffffff8O). Then, when the variable is imlnt -129 (Oxffffff7f). It was converted to byte type value obtained 127 (0x0000007f).

 

Guess you like

Origin www.cnblogs.com/mongotea/p/11980066.html