Automatic elevation of the Java expression type

Source: Internet
Author: User
Tags integer division

Transfer from http://www.blogjava.net/hyperjava/articles/241839.html

When a Java arithmetic expression contains values of more than one primitive type, the data type of the entire arithmetic expression will automatically ascend. Java defines the following automatic promotion rules:
1. All byte, short, and char types will be promoted to the int type.
2. The data type of the entire arithmetic expression is automatically promoted to the same type as the highest-level operand in the expression. The level of the operand is arranged as shown, and the level at the right of the arrow is higher than the type at the left of the arrow.


The following procedure demonstrates a typical error:

Define a short type variable
Short svalue = 5;
The svalue in the expression is automatically promoted to the int type, and the right expression type is int
assigning an int type to a variable of type short will have an error.
svalue = sValue-2;

The type of the sValue-2 expression above will be promoted to the int type, so that the right int value is assigned to the short variable on the left, causing an error.

The following code is the correct example code for automatic elevation of expression type (program manifest above):

byte B = 40;
char c = ' a ';
int i = 23;
Double d =. 314;
The highest-level operand in the right-hand expression is D (double)
The right-hand expression is of type double, so it is assigned to a double-type variable
Double result = B + C + i * D;
Will output 144.222
SYSTEM.OUT.PRINTLN (result);


It must be noted that the type of the expression will strictly persist and the same type as the highest-level operand in the expression, and the following code will divide the two int integers, even if it is not possible to get an int result (as in the program manifest):

int val = 3;
The 2 operands in the right-hand expression are int, so the right-hand expression is of type int
Therefore, although 23/3 cannot be exhausted, it still gets an int integer
int intresult = 23/val;
Will output 7
System.out.println (Intresult);

As can be seen from the above program, when the two integer division operation, if not an integer, the result will be the fractional part truncated integer after rounding.

If the expression contains a string, it is another case: because the plus sign (+) is placed between the string and the base type value, the plus sign is a string join operator instead of an addition operation. look at the following code:

Output String Hello!a7
System.out.println ("hello!" + ' a ' + 7);
Output String 104hello!
System.out.println (' a ' + 7 + "hello!");

for the first expression "hello!" + ' a ' + 7, first the "hello!" + ' A ' operation, will be ' a ' to the string, will be stitched into a string hello!a, followed by the "Hello!a" + 7 operation, which is also a string concatenation operation, Get the result is hello!a7. For the second expression, the ' a ' + 7 addition operation, where ' a ' automatically promoted to the int type, a corresponding ASCII value: 97, from 97 + 7 will be 104, then the 104 + "hello!", 104 will automatically convert to a string, will become two strings of the connection operation, So as to get 104hello!.

——————————————————

PS. Basics are the most important!

Automatic elevation of the Java expression Type (GO)

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.