Automatic data type conversion type of conversion


When the data is not the same type, data type conversion will occur.

Automatic type conversion (implicit)
1. Features : code does not need special treatment, automatically.
2. Rules : data range from small to large .

Casts (explicit)

First, the automatic type transcoding

. 1  public  class Demo01DataType {
 2      public  static  void main (String [] args) {
 . 3          System.out.println (1024); // this is a integer, the default is to type int 
. 4          System.out.println (3.14); // this is a floating-point number, the default is a double
 5          
6          // left is a long type, the right is the default type int, not the same around
 7          // a equals sign represents the assignment, the int constants on the right, to the left side stores long variable
 . 8          // int -> long, in line with the data range from small to large in claim
 9          // this line of code type conversion occurs automatically. 
10          Long num1 = 100 ;
 . 11          System.out.println (num1); //100
 12 is          
13 is          // left type double, the right side is a float type, not about the same as
 14          // float -> double, from small to large compliance rules
 15          // also place automatic conversion 
16          double num2 = 2.5F ;
 . 17          System.out.println (num2); // 2.5
 18          
19          // left is the float type, the type of right is long, left and right are not the same
 20          // long -> float, float range is larger, in line with small to large rule
 21          // also place automatic conversion 
22 is          a float num3 = 30L ;
 23 is          System.out.println (num3); // 30.0 
24      }

operation result

  1024

  3.14

  100

  2.5

  30

  

Guess you like

Origin www.cnblogs.com/chenliqiang/p/11441823.html