Type escalation Convention
In addition to raising BYT and short types to int type, Java defines several types of escalation rules for expressions.
(Type promotion rules). First, as described earlier, the values of all byte and short types are upgraded to int.
Secondly, if an operand is of the long type, the entire expression will be promoted to the long type. If an operand is of the float type,
The entire expression is promoted to the float type. If one of the operands is double, the calculation result is double.
The following program shows how each value in an expression is promoted to match the second parameter of their binary operators:
Class promote {
Public static void main (string [] ARGs ){
Byte B = 42;
Char c = 'a ';
Short S = 1024;
Int I = 50000;
Float F = 5.67f;
Double D =. 1234;
Double result = (F * B) + (I/C)-(D * s );
System. Out. println (f * B) + "+" + (I/c) + "-" + (D * s ));
System. Out. println ("result =" + result );
}
}
Let's look at the type upgrade that occurs in the following program lines:
Double result = (F * B) + (I/C)-(D * s );
In the first expression f * B, variable B is promoted to the float type, and the result of this subexpression is of the float type.
Next, in the subexpression I/c, edge C is upgraded to the int type, and the result of this subexpression is of the int type.
Then, the variable s in the subexpression D * s is upgraded to the double type. The result of this subexpression is of the double type.
Finally, consider the three median values, float type, int type, and double type. The result of adding the float type to the int type is
Float type, and then the float type minus the double type promoted to the double type. The final result of this expression is
Double type.