Implicit conversion and coercion in Java

  1. Implicit conversion: For
    data with a small value range, assign a value to a type with a large value range, and you can directly assign a value to a
    Insert picture description here
    small data type, and a large data type operation, the small data type will be upgraded to a large data type, and then the operation will be performed;
    java byte a = 3; byte b = 4; byte c = (byte)(a+b);//必须加括号 System.out.println(c);
    byte short char When the three types of data are calculated, regardless of whether there is a higher data type, they will be promoted to int, and then the operation will be performed.Java
    has a constant optimization mechanism, which will add 3 and 4 at compile time, and understand 7 is within the byte range is not within range: compile error in range: by compiling
    java byte d = 3 + 4; System.out.println(d);
    Long belt L
    java long num1 = 123456789L;
    2. cast:
    risk, possible loss of precision may occur
    to a large range of data values assigned to variables or other small range of
    front Add the type you want to convert and you can
    remember: For example, if you pour the water from a big bucket into a small bucket, you will probably lose accuracy

Guess you like

Origin blog.csdn.net/qq_42073385/article/details/107698919