java的基本数据类型有八种,其中4种整型,2种浮点类型,1种字符类型和boolean类型
目录
整型(int, long, short, byte)
| 数据类型 | 大小 | 取值范围 | 默认值 | 
| byte | 1字节(byte) | -128 to 127 | 0 | 
| short | 2字节 | -32768 to 32767 | 0 | 
| int | 4字节 | -2,147,483,648 to 2,147,483,647 | 0 | 
| long | 8字节 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L | 
在定义long类型时需要在数据后面加 'L'(推荐大写容易和1区分), 不然就是一个普通的int类型
public class test_max {
    public static void main(String[] args) {
        byte myNum0 = 10;
        short myNum1 = 20;
        int myNum2 = 30;
        //long long_num1 = 3000000000;错误示范
        long long_num2 = 3000000000L;
        System.out.println(long_num2);
    }
}求各类型最大最小值
public class test_max {
    public static void main(String[] args) {
        System.out.println("byte最大值: "+Byte.MAX_VALUE);//127
        System.out.println("byte最小值: "+Byte.MIN_VALUE);//-128
        System.out.println("short最大值: "+Short.MAX_VALUE);//32767
        System.out.println("short最小值: "+Short.MIN_VALUE);//-32768
        System.out.println("int最大值: "+Integer.MAX_VALUE);//2147483647
        System.out.println("int最小值: "+Integer.MIN_VALUE);//-2147483648
        System.out.println("long最大值: "+Long.MAX_VALUE);//9223372036854775807
        System.out.println("long最小值: "+Long.MIN_VALUE);//-9223372036854775808
    }
}把一个大类型赋值给一个小类型需要强制类型转换,同时要注意大类型的值不能超过小类型的取值范围.
public class test_max {
    public static void main(String[] args) {
        byte byte_num1 = 10;
        byte byte_num2 = 10;
        byte byte_num3 = 20;
        int int_num1 = 30;
        int int_num2 = 500;
        //把一个大类型给小类型是不允许的
        //byte_num3 = int_num1; 错误
        byte_num1 = (byte)int_num1;
        byte_num2 = (byte)int_num2;
        System.out.println(byte_num1);//输出30
        System.out.println(byte_num2);//输出-12
    }
}浮点类型(float, double)
| 数据类型 | 大小 | 取值范围 | 默认值 | 
| float | 4字节 | 3.40282347 x 1038 to 1.40239846 x 10-45 | 0.0f | 
| double | 8字节 | 1.7976931348623157 x 10308, 4.9406564584124654 x 10-324 | 0.0d | 
在定义float和double类型的时候,要在数据后面分别加上'f','d' .同样double类型可以接受float的赋值,但是double不能赋值给float.在浮点类型的转换中会损失精度要尽量避免.
public class test_max {
    public static void main(String[] args) {
        float myNum0 = 23.345f;
        double myNum1 = 45.66732d;
        float myNum2 = 23.345f;
        System.out.println(myNum0);//23.345
        System.out.println(myNum1);//45.66732
        myNum1 = myNum2;
        System.out.println(myNum1);//输出23.344999313354492精度损失
    }
}char类型
| 数据类型 | 大小 | 描述 | 默认值 | 
| char | 2字节 | 所有utf - 16字符('A','a','z'....) | \u0000 | 
这里要注意的是,当一个char类型和int类型相加时,char类型会取相应的ascii码值.
'a'的ascii码值是97, 所以这里输出97
    public static void main(String[] args) {
        //unicode-->比ascii码包含的多
        //更加全面了
        char ch0 = 'a';
        char ch1 = 'A';
        char ch2 = '1';
        int num = 0;
        System.out.println(num+ch0);//97
        System.out.println(num+ch1);//65
        System.out.println(num+ch2);//49
    }boolean类型
| 数据类型 | 大小 | 描述 | 默认值 | 
| boolean | N/A | 存储true或者false | false | 
在java中,没有所谓的非0为true,0为false.只有true是真,只有false是假
    public static void main(String[] args) {
        boolean bln = false;
        System.out.println(bln);
        bln = true;
        System.out.println(bln);
    }









