JAVA中的小数称为浮点数


1、有两种类型:

float:单精度浮点数。4个字节。
double:双精度浮点数。8个字节。

2、类型转换
容量小  ------------------------------->  容量大
byte,short,char << int << long << float << double


容量小的类型会自动向容量大的提升;同时,容量大的向小的转,会发生精度丢失。


如下,整数127赋值给float和double的变量时,类型会自动提升,输出结果为浮点数。


public class MyTest {
    public static void main(String[] args) {
        float a=127;
        double b=127;
        System.out.println(a);
        System.out.println(b);
    }
}

输出结果:
127.0
127.0


3、默认时,浮点数为double类型

如下:127.0为double类型,所以赋值给float类型的变量,类型降低,精度丢失,报错。 

public class MyTest {
    public static void main(String[] args) {
        float a=127.0;
        System.out.println(a);
    }
}



如果非要将127.0赋值给a



可以进行强制类型转换,写成 float a=127.0f;