基本数据类型

一 基本数据类型
1 基本类型的表数范围


image.png

float应该只有4字节
2 字面值为整数,只要范围不超过对应类型的示数范围都是可以定义变量类型的;


image.png

3 当整数的字面值表数的极限为int型,当数值的大小超过了int的范围,需要在数值后加L/l标记其为long型整数。
public static void main(String[] args) {
        long l = 1000000000000000L;//1
        long l2 = 100000000;//int-->long
    }

如果1000000000000000后面不加L,会报错The literal 1000000000000000 of type int is out of range

4 字面值为小数默认为double,float型需要在其后加f/F。
float f = 1.0;编译时会报错Type mismatch: cannot convert from double to float
数值的未超过类型表示的范围,可以直接赋值给该类型。
二 类型提升
1 表达式中有 byte,char,short的混合,会自动提升为int。
1.1 运算符连接2个byte/char/short类型的值,或者运算符连接byte,char,short的混合,运算符左右的值会自动提升为int

        byte b = 4;
        char c = 5;
        short s = 3;
        b += b;
        b = b + b ;//Type mismatch: cannot convert from int to byte
        c *= c;
        c = c * c;//Type mismatch: cannot convert from int to char
        s /= s;
        s = s / s;//Type mismatch: cannot convert from int to short
        s = b + c;//Type mismatch: cannot convert from int to short

2 当一个算术表达式中包含多种类型时,整个算术表达式的数据类型将发生自动提升(向上转型),提升到表达式中最高等级操作数同样的类型。
转换关系如图所示:


image.png

3 加号连接字符串和基本数据类型时,加号表示字符串连接符,而不是加法运算。

        System.out.println(1+3+"hello");
        System.out.println("hello"+1+3);

运行结果

4hello
hello13

【参考】
https://blog.csdn.net/u014590757/article/details/79879802

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容