And then brother combed the Java Knowledge points--type conversion between variables (iv)

Source: Internet
Author: User

The type conversions between variables mainly include automatic type conversions and forced type conversions.

1, automatic type conversion: When the size of the small data type and capacity of large data types to do the operation, the small capacity will automatically convert into large-capacity type.

"Knowledge points":

A) char,byte,short---> int---> Long---> Float---> Double---> String

Charc = ' a ';
Shorts = 12;
byteb = 125; //all operations between Char, short, and byte are automatically converted to int    intS1 = C +s; intS2 = b +s; intS3 = C +b; LongL = 1000L; //the operation between int and long is automatically converted to a long    LongL1 = S1 +l; floatf = 12.36f; //the operation between long and float is automatically converted to float    floatF1 = L1 +F; DoubleD = 16952; //the operation between float and double is automatically converted to double    DoubleD1 = f1 + D;

b) That is not a question that has been reflected.

Short A = 3; Compile normal

A = a + 3; Compile error

A + = 3; Compile correctly (reason reference coercion type conversion)

3 is also the default type int type, why the direct assignment to a nothing, and a+3 automatically converted to int type, and then the assignment is compiled wrong?

The problem key point is a constant at 3, which is a compilation error only if the assignment is followed by a variable. So our title is called the type conversion between variables.

int a = 1;   int b = 2;  byte b1 = 1 + 2; // compile correctly byte b2 = a + b; // compilation error (cannot convert from int to byte)  

Then the constant operation also summarizes:

byte B1 = 1 + 126;  // compile correctly byte b2 = 1 + 127;  // compilation error (cannot convert from int to byte) int A = 1 + 1.2;    //  compilation error (cannot convert from double to int) String s2 = 12 + ""; // compile correctly    

(1) 127 in the range of byte, compile correctly, and 128 exceeds the range of byte, compile error.

(2) 1 + 1.2 = 2.2,2.2 is a double type that exceeds the range of int, compilation error.

(3) + "=" (int + string = string), who and string are automatically converted to string.

Summary: Do a constant operation to see the numerical range of the result. You can't simply look at the type range.

C) Written questions:

String a = "abc";  char c = ' a '; // the ASCII code of A is  int i =n; System.out.println (a+c+i); //  Abca12 System.out.println (C+i+a); // 109abc System.out.println (a+i+c); // abc12a  

2. Coercion type conversion: When the capacity is large to convert to small capacity, the coercion type conversion is required. Use the form: (type) to do the casting.

"Knowledge points":

A) Problem of forced type conversion: loss of precision caused

int i =n;   byte by = (byte) I; System.out.println (by);

Output: 128

b) operator operations such as +=,-=,*= are automatically coerced to type conversions.

For example, the above case: A + = 3; It's actually equivalent to short a = (short) a+3; so it's definitely not compiling errors.

And then brother combed the Java Knowledge points--type conversion between variables (iv)

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.