Java type upgrade Convention

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.