search
HomeJavajavaTutorialIn-depth analysis of Java unboxing

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);
  }
}


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);
  }


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() {}
  }


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);
  }
}


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);
  }


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);


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
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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools