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

时间:2021-02-06 12:12:00   收藏:0   阅读:0

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

整数扩展

public class dataTypeExpansion {
    public static void main(String[] args) {
        int a = 10;
        int b = 010; //八进制
        int c = 0x10; //十六进制 0-9 A-F 16

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

输出结果为:

10
8
16

浮点数扩展

public class dataTypeExpansion {
    public static void main(String[] args) {
        float d = 0.1f; //0.1
        double e = 1.0/10; //0.1

        System.out.println(d==e);

        float f = 21313131313131313f; //21313131313131313
        float g = f + 1; //21313131313131314

        System.out.println(f==g);
    }
}

输出结果为:

false
true

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

字符扩展

public class dataTypeExpansion {
    public static void main(String[] args) {
        char h = ‘P‘;
        char i = ‘浦‘;

        System.out.println(h);
        System.out.println((int)h); //强制转换
        System.out.println(i);
        System.out.println((int)i); //强制转换

        char j = ‘\u0061‘;

        System.out.println(j);

        System.out.println("Hello\tWorld");
        System.out.println("Hello\nWorld");
    }
}

输出结果为:

P
80
浦
28006
a
Hello	World
Hello
World

布尔值扩展

Less is more。

代码要精简易读。

if (flag==true){} = if (flag){}

public class dataTypeExpansion {
    public static void main(String[] args) {
        boolean flag = true;

        if (flag==true){} //新手
        if (flag){} //老手
    }
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!