Home Java javaTutorial In-depth analysis of Java unboxing

In-depth analysis of Java unboxing

Jan 24, 2017 pm 01:57 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles