Home > Java > javaTutorial > In-depth analysis of Java unboxing

In-depth analysis of Java unboxing

高洛峰
Release: 2017-01-24 13:57:28
Original
1181 people have browsed it

Let’s look at a piece of code first:

public class Main{
  public static void main(String[] args){
 
    Integer num1 = 100;
    Integer num2 = 100;
    Integer num3 = 200;
    Integer num4 = 200;
 
    '''//输出结果'''
    System.out.println(num1==num2);
    System.out.println(num3==num4);
  }
}
Copy after login


Guess what the result is?

Many people will think that the results are all true, but the results are not like this

true
false

Why is this the result? If you use memory to interpret the results, num1 and num2 point to the same object, while num3 and num4 point to different objects. Next, I will tell you why. Take a look at the source code of the valueof method of the Integer type:

public static Integer valueOf(int i) {
  assert IntegerCache.high >= 127;
  if (i >= IntegerCache.low && i <= IntegerCache.high)
    return IntegerCache.cache[i + 128];
  return new Integer(i);
  }
Copy after login


The implementation of IntegerCache:

&#39;&#39;&#39;// IntegerCache,一个内部类,注意它的属性都是定义为static final&#39;&#39;&#39;
  private static class IntegerCache {
    static final int high; &#39;&#39;&#39;//缓存上界,暂为null&#39;&#39;&#39;
    static final Integer cache[]; &#39;&#39;&#39;//缓存的整型数组&#39;&#39;&#39;
 
    &#39;&#39;&#39;// 块,为什么定义为块&#39;&#39;&#39;
    static {
      final int low = -128; &#39;&#39;&#39;// 缓存下界,不可变了。只有上界可以改变&#39;&#39;&#39;
 
      &#39;&#39;&#39;// high value may be configured by property&#39;&#39;&#39;
      &#39;&#39;&#39;// h值,可以通过设置jdk的AutoBoxCacheMax参数调整(以下有解释),自动缓存区间设置为[-128,N]。注意区间的下界是固定&#39;&#39;&#39;
      int h = 127;
 
      if (integerCacheHighPropValue != null) {
        &#39;&#39;&#39;// Use Long.decode here to avoid invoking methods that&#39;&#39;&#39;
        &#39;&#39;&#39;// require Integer&#39;s autoboxing cache to be initialized&#39;&#39;&#39;
        // 通过解码integerCacheHighPropValue,而得到一个候选的上界值&#39;&#39;&#39;
        int i = Long.decode(integerCacheHighPropValue).intValue();
        &#39;&#39;&#39;// 取较大的作为上界,但又不能大于Integer的边界MAX_VALUE&#39;&#39;&#39;
        i = Math.max(i, 127);  
        &#39;&#39;&#39;// Maximum array size is Integer.MAX_VALUE&#39;&#39;&#39;
        h = Math.min(i, Integer.MAX_VALUE - -low);
      }
      high = h; &#39;&#39;&#39;//上界确定&#39;&#39;&#39;
      &#39;&#39;&#39;// 就可以创建缓存块,注意缓存数组大小&#39;&#39;&#39;
      cache = new Integer[(high - low) + 1]; //
      int j = low;
      for(int k = 0; k < cache.length; k++)
        cache[k] = new Integer(j++); &#39;&#39;&#39;// -128到high值逐一分配到缓存数组&#39;&#39;&#39;
    }
 
    private IntegerCache() {}
  }
Copy after login


It can be seen from these two pieces of code that when creating an Integer type object through the valueof method, the value range is [-128,127], and the value is in this range , the pointer points to an object reference that already exists in IntegerCache.cache. When the value exceeds this range, a new object will be created.

One thing to note is that not all types are in this range. Look at the Double type:

public class Main{
  public static void main(String[] args){
 
    Double i1 = 100.0;
    Double i2 = 100.0;
    Double i3 = 200.0;
    Double i4 = 200.0;
 
    System.out.println(i1==i2);
    System.out.println(i3==i4);
  }
}
Copy after login


Finally The output result:

false
false

Specific reason for this result, you can take a look at the implementation of the Double valueof method in the source code, which is different from the Integer valueof method. This is because the number of integer values ​​within a certain range is limited, but floating point numbers are not.

Note that the implementation of the valueOf method of Integer, Short, Byte, Character, and Long classes is similar.
The implementation of the valueOf method of Double and Float is similar.

Pulled down one, the Boolean type result has two True or False. Look directly at the source code:

public static Boolean valueOf(boolean b) {
    return (b ? TRUE : FALSE);
  }
Copy after login


and TRUE and FALSE are defined like this:

public static final Boolean TRUE = new Boolean(true);
 
&#39;&#39;&#39;/** &#39;&#39;&#39;
&#39;&#39;&#39;* The <code>Boolean</code> object corresponding to the primitive &#39;&#39;&#39;
&#39;&#39;&#39;* value <code>false</code>. &#39;&#39;&#39;
&#39;&#39;&#39;*/&#39;&#39;&#39;
public static final Boolean FALSE = new Boolean(false);
Copy after login


The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more articles related to in-depth analysis of Java unboxing, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template