Data conversion (automatic conversion + forced conversion)

/*
Automatic conversion
1. Explicit: The code does not need to be processed, and the conversion
  rules are directly completed: the data range is from small to large
*/
public class demo05{   public static void main(String[] args){      System.out.println(30); //Directly output an integer, the default is Int type      System.out.println(3.12);//Directly output a decimal, the default is double type      long nume1=100;//Data conversion between integers      //(1) Left side It is of long type, and the right side is of int type by default.      //(2) The equal sign is assigned, and the right side is assigned to the left.     //(3) int——>long, which satisfies the data range of the left size and the right size.     //(4) This line of code occurs Automatic conversion      System .out.println(nume1);//100      double nume2=3.14159267F;//Data conversion between floating point numbers      //(1) double on the left, float on the right;     //(2) from double ->float; To meet the data range of left size and right size, automatic type conversion also occurs      System.out.println(nume2);      float nume3 =30L;














     //(1) From long->float, the range of float is greater than long, which satisfies the data range of large left and right small
    //(2) This line of code is automatically converted
      System.out.println(nume3);
  }
}

/*Forced conversion
  1. Features: The code needs to be processed in a special format and cannot be completed automatically
  2. Format: type with a small range + variable name with a small range = (type with a small range) + data with a large range
  Note:
  1 mandatory Conversion is generally not recommended because of possible data overflow and loss of precision.
  2. The three types of byte/short/char can be used for mathematical operations
  . 3. When the three types of byte/short/char are operated, they will be first referred to the int type
  . Data type conversion
*/
public class demo06{   public static void main(String[] args){       //The left side is int type, the right side is long       //long->int, not from small to large, can not be automatically converted after finishing       / /Format; data type with small range + variable name with small range = (data type with small range) + original data with large range       int nume1=(int)100L;       System.out.println(nume1);       /*       //long ->int (cast)       int nume2=6000000000;       System.out.println(nume2);//1705032704 overflow       */










 

      //double->int (cast)
      int nume2=(int)3.99;
      System.out.println(nume2);//3 (this is not rounding, but rounding off decimal places) loss of precision


      char nuem3='A';//This is a character variable 'A'
      System.out.println(nume3+1);//66 that is, the capital letter A is treated as 65 for processing
      //The bottom layer of the computer will use A number (binary) to represent the character 'A', which is 65
      //Once the char is mathematically operated, the character will be translated into a number according to certain rules
      
      /*
      byte number4=40;//The size of the value on the right side Cannot exceed the value range on the left
      byte nume5=50;
      byte result1=nume4+nume5;//(both will be first mentioned as int type) int+int->int
       System.out.println(result1);
       */
       /*
       Here An error will be reported
       */
      //The correct way of writing is as follows:
      byte nume4=40;//The size of the value on the right cannot exceed the value range on the left
      byte nume5=50;
      int result1=nume4+nume5;
      //byte+bute->int +int->int
       System.out.println(result1);//90
       short number6=40;
       //byte+short->int+int->int
       //int is cast to
       short

       //Minimum data type + minimum data variable = (minimum data type) + (data with a large original range)
       shiort result2=(short)(nume5+nume6);//90
  }
}
       

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_45650003/article/details/119126369