美文网首页
从零开始的JDK之旅 day03

从零开始的JDK之旅 day03

作者: 从零开始的程序猿生活 | 来源:发表于2020-11-07 21:57 被阅读0次

包装类(\color{blue}{Integer、Boolean}类)

常见问题:

        /* 1、常见问题 */
        Integer i1 = 127;
        int i2 = 127;
        Integer i3 = Integer.valueOf(127);
        Integer i4 = new Integer(127);
        Integer i5 = 128;
        Integer i6 = Integer.valueOf(128);

        System.out.println(i1 == i2); // true 因为i2是基本数据类型 所以比较时 会将i1 转成基本数据类型 比较二者的值
        System.out.println(i2 == i3); // true 同上 Integer.valueOf(127) 就是将127 转成包装类
        System.out.println(i1 == i3); // true Integer内部维护了一个范围[-128~127]的缓冲区,如果值在这个范围内会直接返回缓冲区中的对象,所以为true
        System.out.println(i5 == i6); // false 取值范围不在缓冲区中,将会创建新的对象。
        System.out.println(i1 == i4); // false 因为new 关键字会在堆中开辟出一片新的空间所以i4 和 i1 存的引用并不相同

Integer有个内部类,维护了从[-128~127(最大值可调)]的缓存区,虚拟机一开始就会new出127-(-128)+1个对象,如果值在这个范围内直接将同一个对象返回,否则会创建新的对象。

Integer的缓存类实现:
    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }
Long的缓存类实现:
    private static class LongCache {
        private LongCache(){}

        static final Long cache[] = new Long[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Long(i - 128);
        }
    }

Long 、Short、Byte 的缓存类的最大值是固定的,不可配的,范围是[-128~127],只有Integer的可以配置

定义了两个静态常量,使用的16进制。

    /**
     * A constant holding the minimum value an {@code int} can
     * have, -2<sup>31</sup>.
     */
    @Native public static final int   MIN_VALUE = 0x80000000; // -2147483648 

    /**
     * A constant holding the maximum value an {@code int} can
     * have, 2<sup>31</sup>-1.
     */
    @Native public static final int   MAX_VALUE = 0x7fffffff; // 2147483647

Integer类中提供了进制转换的静态方法:
toBinaryString(int i) 将i转成二进制字符串
toHexString(int i) 将i转成十六进制字符串

转换字符串方法 :
public static Integer valueOf(String s) :
内部调用的parseInt方法获取到int类型的值然后创建一个Integer对象,返回值为Integer。
public static int parseInt(String s) :
返回值是一个int 基本数据类型的值。

    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }
    public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }

相关文章

网友评论

      本文标题:从零开始的JDK之旅 day03

      本文链接:https://www.haomeiwen.com/subject/eldqbktx.html