数字字面量的改进

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qmqm011/article/details/83025519

Java 7之前支持十进制、八进制、十六进制,Java 7新增了对二进制的支持。另外,Java 7支持在数字中使用下划线(_)作为分隔符,如(1_000_000)。注意:下划线仅仅能在数字中间,编译时编译器自己主动删除数字中的下划线。

    public static void main(String[] args) {
        int a = 15; // 二进制
        System.out.println(a); // 10

        int b = 017; // 八进制:
        System.out.println(b); // 1*8^1 + 7*8^0=15

        int c = 0XAB; // 十六进制,字母大小写都可以
        System.out.println(c); // 10*16^1 + 11*16^0=171

        int d = 0B1111; // 二进制,Java 7新支持,其中b大写小写都可以
        System.out.println(d); // 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 = 15

        int e = 1_000_000; // 数字中可加入下划线作为分隔符
        System.out.println(e); // 1000000

    }

猜你喜欢

转载自blog.csdn.net/qmqm011/article/details/83025519
今日推荐