Java basic data types and automatic type promotion

Source: Internet
Author: User

8 Basic data types for 1.Java and their space size: Boolean 8bit/1byte byte 8bit/1byte char 16bit/2byte short 16bit/2b     yte float 32bit/4byte int 32bit/4byte long 64bit/8byte double 64bit/8byte 2.Java automatic type conversion 1) The two types are compatible with each other 2) convertedPurpose TypeThe space occupied must be greater than the conversionSource Type

Forward process: automatic conversion from low byte to high byte

    Byte->short->int->long->float->double

Reverse process: Using casts, you may lose precision.

int a= (int) 3.14;

3. Java Data Type automatic promotion (note the following is the two-dollar operator) Java defines several type-promotion rules that are used for expressions:1) all byte type. Short and char types will be promoted to type int (Exception: final modified short, char variables are not automatically promoted when added. )2) If an operand is a long shape, the result is a long type;   3) If an operand is of type float, the result is float type; 4) If an operand is a double type, the result of the calculation is double type; another form of induction (Java Core Technology Volume I P43):If one of the two operands is a double type, the other operation is converted to a double type.  Otherwise, if one of the operands is of type float, the other will be converted to the float type.  Otherwise, if one of the operands is of type long, the other is converted to a long type. Otherwise, the two operands are converted to the int type. eg 1:
1Promote.javaclassPromote2     {  3          Public Static voidMain (String args[])4         { 5             byteb = 50;6             Charc = ' a ';7              Shorts = 1024; 8             inti = 50000; 9             floatF =5.67f; Ten             DoubleD =0.1234;  One             Doubleresult = (f * b) + (I/C)-(d *s);  A          }  -}
Explanation:In the first expression, F * B, B is promoted to the float type, and the result of the subexpression is also promoted to float.      In the second expression i/c, the variable c is promoted to the int type, and the result of that subexpression is promoted to the int type.      In the third expression, d * s, the variable s is promoted to a double type, and the result of that subexpression is promoted to double. Finally, the three result types are the Float,int and double types, and the last result of the expression that you want to subtract is the double type.

5) mo The floating-point type has a suffix of "f" or "F" for the Double,float data type.

6) A long type has a suffix of "l" or "L".

EG 2:

1 byte a = 1; 2 3 byte b = 2; 4 5 a = a+b;      // compilation error automatic type promoted to int 6 7 A + = b;       // self-add no automatic type promotion problem

  Converting a high byte to a low byte requires a forced type conversion. byte c= (byte) a+b;

EG 3:

1 byte b1=1,b2=22finalbyte b4=4,b5=63 b6=b4+   4 b3= (b1+B2);   //Compile error will occur  5 System.out.println (B3+B6);

Variables that are not final modified are automatically promoted to type int, incompatible with the target type byte and require a cast (downward transition).

4. Java forced type conversion format: Target type variable = (target type) source type variable/constant eg:int i=5; byte j= (int) i;Note:The type of the target type and the source type variable is never changed in coercion type conversionEasy Error Point:BYTE B;        b=3; B= (Byte) b*3 //Compile error because (byte) the operation level is higher than *, so the B is converted first and thenB= (Byte) (b*3) //CorrectReference post: http://blog.csdn.net/liang5630/article/details/37935383http://blog.sina.com.cn/s/blog_66e7f79b0100ysd3.html

Java basic data types and automatic type promotion

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.