数据类型扩展及面试题目讲解

数据类型的扩展 P24

---

1.整数扩展

进制: 二级制: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("====================================");


2.浮点数扩展

特点:float 有限 离散 舍入误差 大约 接近但是不等于

重要的事情说三遍!!

最好完全避免使用浮点数进行比较;
最好完全避免使用浮点数进行比较;
最好完全避免使用浮点数进行比较;

 float f = 0.1f;
        double d = 1.0/10;
        System.out.println(f==d);  // false !!!!
        System.out.println(f);
        System.out.println(d);

当浮点数数值过大时,两个值不同的数,相等

float d1 = 234123541234123421f;
        float d2 = d1 + 1;
        System.out.println(d1);
        System.out.println(d2);

d1 = d2


3.字符扩展

强制类型转换

char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);
        System.out.println((int)c1); //强制类型转换
        System.out.println(c2);
        System.out.println((int)c2);//强制类型转换

所有的字符本质上还是数字。
编码: Unicode表:(97 =a ,65 = A)2字节 范围(0 ~ 655536) 二的十六次方

4.字符串扩展

搞清楚赋值给了一个对象(从内存分析)还是变量。

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

        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc==sd);

5.布尔值扩展

 boolean flag = true;
        if(flag == true){}   // 新手上路
        if(flag){}  // 老手上路
        //less is more! 代码要精简易读
posted @ 2022-06-22 17:08  Haziy  阅读(49)  评论(0编辑  收藏  举报