6, the basic data type conversion

boolean type is not converted to other data types. (Other types can not be converted to boolean)
integer, character, floating point type data conversion in the mixing operation, the following guidelines conversion:
a small capacity to large capacity type is automatically converted data type; data type by volume order of size:
byte, Short, char- "INT-" Long- "float-" Double
byte, not interchangeable between short, char, they are in the calculation of the three first converted to an int type
large-capacity data type conversion It is a small capacity type of data, to add character cast, but may result in reduced accuracy or overflow; to be extra careful when using
with a plurality of operation when mixing data types, the system automatically converts all of the first data to the maximum capacity that a data type, and then calculated.
Real constants (eg: 1.2) to double default
integer constants (eg: 123) The default is int

    TestConvert.java 
    public class TestConvert { 
      public static void main (String args []) { 
        int I = 123; 
        int I2 = 456; 
        double D1 = (I1 + I2) * 1.2; // will be converted to double arithmetic 
        float f1 = (float) ((i1 + i2) * 1.2); // need to strengthen the system for the conversion character 
        byte B1 =. 1; 
        byte B2 = 2; 
        byte B3 = (byte) (B1 + B2); // the system to int type of operation, it is necessary to strengthen the system operator conversion 

        Double D2 = 1e200; 
        a float F2 = (a float) D2; overflow occurs // 
        System.out.println (F2); 

        a float F3 = 1.23f; // must be added F 
        Long L1 = 123 ; 
        Long L2 = 300000000000L; // must be added L 
        float F = L1 + L2 + F3; // will be converted to float operation 
        long l = (long) f; // will cast truncation of the fractional portion (not round)

      }
    }
public class TestConvert2 {
  public static void main (String[] args) {

    int i = 1;
    int j = 2;
    float f1 = (float)0.1;//0.1f
    float f2 = 123;
    long l1 = 12345678;
    long l2 = 888888888888L;
    double d1 = 2e20;
    double d2 = 124;
    byte b1 = 1;
    byte b2 = 127;
    j = j+10;
    i = i/10;
    i = (int)(i*0.1);
    char c1 = 'a';
    char c2 = 125;
    byte b = (byte)(b1-b2);
    char c = (char)(c1+c2-1);
    float f3 = f1+f2;
    float f4 = (float)(f1+f2*0.1);
    double d = d1*i+j;
    float f = (float)(d1*5+d2);

  }
}

  

Guess you like

Origin www.cnblogs.com/hlc-123/p/11019409.html