首页 > 编程知识 正文

java integer valueof,java integer.valueof方法

时间:2023-05-06 07:30:49 阅读:247248 作者:4282

我们先看一段代码:

public static void main(String[] args) {Integer i1 = 20;Integer i2 = 20;System.out.println(i1 == i2);System.out.println(i1.equals(i2));}

接着往下看之前,大家可以先自己想一想输出结果是什么。

输出结果:ture true

如果代码换成这样呢:

public static void main(String[] args) {Integer i1 = 200;Integer i2 = 200;System.out.println(i1 == i2);System.out.println(i1.equals(i2));}

两次输出结果会有什么不一样呢?

这次输出的是 false true

为什么会是这样呢?
首先,我们看一下双等号“ == ”和equals()方法的不同之处。
双等号“ == ”,对于基本数据类型,比较的是它们的值。
对于非基本类型,比较的是它们在内存中的存放地址,或者说是比较两个引用是否引用内存中的同一个对象。

equals()是在Object基类中定义的方法。
这个方法的初始行为是比较对象的内存地址,但在一些类库中,这个方法被覆盖掉了,如String,Integer,Date等。在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址了。
对于复合数据类型之间进行equals比较,在没有覆写equals方法的情况下,它们之间的比较还是基于他们在内存中的存放位置的地址值的,因为Object的equals方法也是用双等号“ == ”进行比较的,所以比较后的结果跟双等号“ == ”的结果相同。

Integer.class中源代码:

public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; }

可以看到,Integer中比较的是值。

因此,两次i1.equals(i2)返回的都是true。

为什么 i1 == i2两次输出结果不一致呢?我们分析一下代码:

Integer i1 = 20,Integer是包装类型,20是整形常量,所以这里涉及到自动装箱过程,i1是Integer的引用,指向20这个整形常量所占用的内存。

Integer i1 = 20 或者Integer i1 = 200的时候,会调用 Integer中的 valueOf()方法。我们看一下这个方法的源码:

/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }

这段代码的作用是把int类型转换成Integer,也就是装箱,它返回一个 Integer对象。
这里又涉及到IntegerCache,对于这段静态代码(这里JDK版本是JDK1.8):

/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */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() {} }

这段代码的作用是,在Integer类装载入内存时,把[-128, 127]范围内的整型数据装包成Integer类,并将其对应的引用放入到cache数组中。

从上面的源码可以看出,valueOf()在返回之前,会进行判断,判断当前 i的值是否在 -128到127之间。
如果存在,则直接返回引用,不再重新开辟内存空间。
如果不存在,就创建一个新的对象。
利用缓存,这样做既能提高程序执行效率,还能节约内存。

Integer i1 = 20; Integer i2 = 20; 因为 IntegerCache中已经存在此对象,直接返回引用,引用相等并且都指向缓存中的数据,所以这时候i1 == i2返回true。

Integer i1 = 200; Integer i2 = 200;因为i1,i2的值大于127,不在[-128, 127]范围内,所以虚拟机会在堆中重新new一个 Integer对象来存放200,创建两个对象就会产生两个这样的空间。两个空间的地址不同,返回到栈中的引用的值也就不同,所以这时候i1 == i2返回false。

以上是我的一些踩坑记录,有什么不足和错误之处,欢迎指正。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。