java 数据类型常见的类型(面试笔试容易考)

java数据类型常见的类型(面试笔试容易考)

各个类型的简单使用

package base;

public class Demo1 {
    public static void main(String[] args) {
        //整数扩展:进制
        //二进制0b
        //十进制
        //八进制0
        //十六进制0x
        int i = 10;
        int i2 = 010;//八进制
        int i3 = 0x10;//十六进制
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);


        System.out.println("****************************");
        //浮点数扩展
        //float、double
        float f = 0.1f;
        double d = 1.0/10;
        System.out.println(f);
        System.out.println(d);
        System.out.println(f==d);//false
        System.out.println("****************************");
        float d1 = 12312312123f;
        float d2 = d1 + 1;
        float d3 = 123f;
        float d4 = d3 + 1 ;
        System.out.println(d1 == d2 );//true
        System.out.println(d3 == d4 );//false
        //浮点数 有限 离散 舍入误差 大约 接近但不等于
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较

        System.out.println("****************************");

        //字符扩展
        char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);//a
        System.out.println(c2);//中
        System.out.println((int)c1);//91
        System.out.println((int)c2);//20013
        //所有的字符本质都是数字
        //unicode 编码  2字节 0-65536

        System.out.println("****************************");

        String sa = new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa == sb );//false

        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc == sd );//true
        // 从内存分析

        // 布尔值扩展
        boolean flag = true;
        if (flag == true){}
        if (flag){}
        //一个意思
    }
}



类型转换问题

package base;

public class demo2 {
    public static void main(String[] args) {
        // 强制类型转换  高----低
        // 自动类型转换  低----高


        // 高----低: int----byte
        int i = 128;
        byte b = (byte)i;
        //将int强制转换为byte,但是byte:0-127,补码
        System.out.println(i);//128
        System.out.println(b);//-128


        //低----高:int----double
        double d = i;
        System.out.println(d);//128.0

        //精度问题

        System.out.println("********************");
        System.out.println((int)23.7);//23
        System.out.println((int)-45.86f);//-45

        //溢出问题
        System.out.println("********************");
        int money = 10_0000_0000;
        int years  =  20;
        int total = money*years;
        System.out.println(total);//-1474836480

        long total2 = money*years;
        //money和years都是int,默认total2也是int,相当于计算结果默认为int,再转化为long
        System.out.println(total2);//-1474836480

        long total3 = money*(long)years;
        System.out.println(total3);//20000000000


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值