Automatic type upgrade in java expressions

Source: Internet
Author: User
Java expressions-general Linux technology-Linux programming and kernel information. In addition to value assignment, there is another type transformation: In the expression. If you want to know the reason, let's look at it. In an expression, the exact requirement on the intermediate value sometimes exceeds the range of any operand. For example, consider the following expression: byte a = 40; byte B = 50; byte c = 100; int d = a * B/c; the result of the intermediate item a * B is easily beyond the range of any of its byte operands. To solve this problem, Java automatically upgrades the operands of each byte or short type to the int type when analyzing expressions. This means that the subexpression a * B is executed using an integer instead of a byte. In this way, even if both variable a and variable B are specified as byte type, the result 2000 of the 50*40 intermediate expression is valid. Automatic type improvement is advantageous, but it also causes confusing compilation errors. For example, this seemingly correct program causes a problem: byte B = 50; B = B * 2; // Error! Cannot assign an int to a byte! This program tries to store a fully valid byte type value of 50*2 to a byte type variable. However, when the expression is evaluated, the operand is automatically upgraded to the int type, and the calculation result is also upgraded to the int type. In this way, the expression is now in int type, and cannot be assigned as a byte type without forced conversion. Indeed, in this particular case, the assigned value will still be suitable for the target type. When you understand the consequences of overflow, you should use an explicit forced type conversion, for example, byte B = 50; B = (byte) (B * 2 ); it generates a correct value of 100. 3.10.1 in addition to raising byte and shorts types to int type, Java defines several type promotion rules (type promotion rules) applicable to expressions ). 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 will be 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 the expression is promoted to match the second parameter of their respective 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 take a look at the type upgrade in the following program rows: double result = (f * B) + (I/c) )-(D * s); in the first sub-expression f * B, variable B is promoted to the float type, and the result of this Sub-expression is of the float type. Next, in the subexpression I/c, variable c is promoted 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, and 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 float Type plus int type is float type. Then, the float type minus the double type promoted to the double type. The final result of this expression is the double type.
Related Article

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.