• java Integer.valueOf 和 Integer.parseInt 和 new Integer区别及注意事项


    先看一下下面的结果

    1.System.out.println(127==127); //true , int type compare
    2.System.out.println(128==128); //true , int type compare
    3.System.out.println(new Integer(127) == new Integer(127)); //false, object compare
    4.System.out.println(Integer.parseInt("128")==Integer.parseInt("128")); //true, int type compare
    5.System.out.println(Integer.valueOf("127")==Integer.valueOf("127")); //true ,object compare, because IntegerCache return a same object
    6.System.out.println(Integer.valueOf("128")==Integer.valueOf("128")); //false ,object compare, because number beyond the IntegerCache
    7.System.out.println(Integer.parseInt("128")==Integer.valueOf("128")); //true , int type compare

    解释

    int整型常量比较时,== 是值比较,所以1,2返回true。1,2是值比较。

    new Integer() 每次构造一个新的Integer对象,所以3返回false。3是对象比较。

    Integer.parseInt每次构造一个int常量,所以4返回true。4是值比较。

    Integer.valueOf返回一个Integer对象,默认在-128~127之间时返回缓存中的已有对象(如果存在的话),所以5返回true,6返回false。5,6是对象比较。

    第7个比较特殊,是int 和 Integer之间的比较,结果是值比较,返回true。

    总结

    对于整型的比较,首先判断是值比较还是对象比较,值比较肯定返回true,有一个是值就是值比较。对象比较,则看对象是怎么构造出来的,如果是采用new Integer方式,则每次产生新对象,两个new出来的Integer比较肯定返回false,如果是Integer.valueOf方式的话,注意值的区间是否在-128~127之间,如果在,则构造的相同值的对象是同一个对象,==比较后返回true,否则返回false。

    所以,对于值比较==放心没问题,对于Integer的比较最好用equals方法比较对象内容,当然注意先判断Integer是否不为null。

    知识扩展

    针对Integer.valueOf源码分析一下

    1.我们调用的Integer.valueOf方法, 它先调用parseInt转成int型数值,再调它自己的重载方法

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

    2.Integer.valueOf重载方法,根据数值i的大小,决定是否从缓存中取一个Integer对象

     public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }

    3.Integer的构造函数,非常简单

      public Integer(int value) {
            this.value = value;
        }

    4.IntegerCache静态类,是Integer的内部类,三个属性(一个缓存的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"); //读取VM参数
                if (integerCacheHighPropValue != null) {
                    try {
                        int i = parseInt(integerCacheHighPropValue); //配置值转换成int数值
                        i = Math.max(i, 127);  //和127比较,取较大者
                        // Maximum array size is Integer.MAX_VALUE(控制缓存数组的大小,最大为整型的最大值,这样一来,h值就必须小于整型最大值,因为要存 -128~0这129个数嘛)
                        h = Math.min(i, Integer.MAX_VALUE - (-low) -1); //实际就是 h=Math.min(i,Integer.MAX_VALUE-129),正整数能缓存的个数
                    } 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++);  //将一些int常量缓存进Integer对象数组缓存中去
    
                // range [-128, 127] must be interned (JLS7 5.1.7)
                assert IntegerCache.high >= 127; //如果小于127,抛异常
            }
    
            private IntegerCache() {}
        }

     看完上述Integer.valueOf源码后,你就会发现,默认的Integer缓存int常量池是可以配置的,配置方法是添加VM参数,加: -Djava.lang.Integer.IntegerCache.high=200

    Intellij IDEA 运行配置的VM Options选项中添加参数即可。

  • 相关阅读:
    英语中容易混淆单词
    Centos7 中安装 Redis 6.0.6
    JAVA基础- 为啥 Integer 中100=100 为true 而200=200则为false
    Java面试BIO,NIO,AIO 的区别
    Maven 中 dependencyManagement 干嘛用的
    代理实现流程
    使用do...while的方法输入一个月中所有的周日
    JavaScript---while和do while的区别
    微信小程序和H5之间相互跳转
    微信小程序进入广告实现
  • 原文地址:https://www.cnblogs.com/hdwang/p/7009449.html
Copyright © 2020-2023  润新知