Data type conversions:
Sometimes, a program needs to have a data type, such as int + float, and the result is a float, where int is converted to float type, which is a legal conversion.
The legitimate transformations in Java are as follows:
Red indicates no loss of information for the conversion, and Orange indicates a potential loss of progress.
PackageTestbotoo; Public classShuzhileixingzhuanhuan { Public Static voidMain (string[] args) {intn = 123456789; floatf = n;//convert int type to float typeSystem.out.println (f); //Forcing type conversions Doublex = 9.9997; intNX = (int) x;//Strong turn operation, so that you can truncate the decimal part NX = 9//Of Course we can also perform rounding operations, using the Math.Round () method DoubleX1 = 9.9997; intNX1 = (int) Math.Round (x1); System.out.println (NX1); //Operation LevelA && B | | C equivalent to (a && b) | |c a+ = b + = c equivalent to A + = (b + =c) Operation level as shown in:}}
Conversion rules:
* If one of the two operands is a double type, the other operand is converted to a double type
* Otherwise, if one of the operands is of type float, the other operand will be converted to float type
* Otherwise, if one of the operands is of type long, the other operand will be converted to a long type
* Otherwise two operands will be converted to int type
Excerpt from: "Java Core technology" 3.5.5
Forced swap:
double x = 9.9997; int NX = (int// Strong turn operation, so it can truncate the decimal part NX = 9 // Of course we can also perform rounding operations, Used is the Math.Round () method Double x1 = 9.9997; int nx1 = (int) math.round (x1); System.out.println (NX1);
The level of the operator:
A && B | | C equivalent to (a && b) | | C + = b + = c equivalent to a + = (b + = c)
As shown in the following:
Java===java Basic Learning (3)---Data type conversions, operator levels, enumeration types