3. Data type conversion

1. Data type conversion

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

  • Automatic type conversion (implicit)
  • Casts (display)

1.1 automatic type conversion (implicit)

Features: Code does not require special handling, automatically
rule: data range from small to large (not necessarily related to the number of bytes)

public class Demo01DataType {
    public static void main(String[] args) {
        System.out.println(1024);//这就是一个整数,默认就是int类型
        System.out.println(3.14);//这就是一个浮点数,默认就是double类型
        
        //1.整数和整数
        //左边是long,右边默认int
        //int--->long,符合数据范围从小到大,所以int可以自动转换成long
        long num1=100;
        System.out.println(num1);//100
        
        //2.浮点数和浮点数
        //左边是double,右边默认float
        //float--->double,符合数据范围从小到大,所以float可以自动转换成double
        double num2=2.5F;
        System.out.println(num2);
        
        //3.整数和浮点数
        //左边是float,右边是long
        //long--->float,符合范围从小到大
        float num3=30L;
        System.out.println(num3);//30.0
    }
}

Guess you like

Origin www.cnblogs.com/deer-cen/p/12118089.html