Home Java javaTutorial Detailed explanation and example code of Java boxing and unboxing

Detailed explanation and example code of Java boxing and unboxing

Jan 24, 2017 pm 02:13 PM

Java boxing and unboxing detailed explanation

Foreword:

To understand the concepts of boxing and unboxing, you must understand Java data types

Boxing: Primitive types are wrapped with their corresponding reference types, giving them the properties of objects. Int is packaged into Integer, float is packaged into Float

Unboxing: Contrary to boxing, reference type objects are simplified into value type data

Integer a = 100;         这是自动装箱 (编译器调用的是static Integer valueOf(int i))
int   b = new Integer(100); 这是自动拆箱
Copy after login

Look at the following piece of code

m1

public class DataType {
  
  public static void main(String args[]) {
    DataType dt = new DataType();
    dt.m11();
    dt.m12();
      
  }
  
  public void m11() {
    Integer a = new Integer(100);
    Integer b = 100;
    System.out.println("m11 result " + (a == b));
  }
  
  public void m12() {
    Integer a = new Integer(128);
    Integer b = 128;
    System.out.println("m12 result " + (a == b));
  }
  
    
}
Copy after login

What is the print result?

m11 result false
m12 result false
Copy after login

"==" compares addresses, and the addresses of the two objects a and b are different, that is, they are two objects, so they are both false

Parse the bytecode through javap, the content is as follows

public void m11();
 Code:
  0:  new   #44; //class java/lang/Integer
  3:  dup
  4:  bipush 100
  6:  invokespecial  #46; //Method java/lang/Integer."<init>":(I)V
  9:  astore_1
  10: bipush 100
  12: invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  15: astore_2
  16: getstatic    #53; //Field java/lang/System.out:Ljava/io/PrintStream;
  19: new   #59; //class java/lang/StringBuilder
  22: dup
  23: ldc   #61; //String m11 result
  25: invokespecial  #63; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
  28: aload_1
  29: aload_2
  30: if_acmpne    37
  33: iconst_1
  34: goto  38
  37: iconst_0
  38: invokevirtual  #66; //Method java/lang/StringBuilder.append:(Z)Ljava/la
ng/StringBuilder;
  41: invokevirtual  #70; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
  44: invokevirtual  #74; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
  47: return
  
public void m12();
 Code:
  0:  new   #44; //class java/lang/Integer
  3:  dup
  4:  sipush 128
  7:  invokespecial  #46; //Method java/lang/Integer."<init>":(I)V
  10: astore_1
  11: sipush 128
  14: invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  17: astore_2
  18: getstatic    #53; //Field java/lang/System.out:Ljava/io/PrintStream;
  21: new   #59; //class java/lang/StringBuilder
  24: dup
  25: ldc   #82; //String m12 result
  27: invokespecial  #63; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
  30: aload_1
  31: aload_2
  32: if_acmpne    39
  35: iconst_1
  36: goto  40
  39: iconst_0
  40: invokevirtual  #66; //Method java/lang/StringBuilder.append:(Z)Ljava/la
ng/StringBuilder;
  43: invokevirtual  #70; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
  46: invokevirtual  #74; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
  49: return
</init></init></init></init>
Copy after login

 

m2

public class DataType {
  
  public static void main(String args[]) {
    DataType dt = new DataType();
    dt.m21();
    dt.m22();
  }
  
  public void m21() {
    Integer a = new Integer(100);
    Integer b = new Integer(100);
    System.out.println("m21 result " + (a == b));
  }
  
  public void m22() {
    Integer a = new Integer(128);
    Integer b = new Integer(128);
    System.out.println("m22 result " + (a == b));
  }
  
    
}
Copy after login

The print result is

m21 result false
m22 result false
Copy after login

a and b are still two objects

javap parsing content

public void m21();
 Code:
  0:  new   #44; //class java/lang/Integer
  3:  dup
  4:  bipush 100
  6:  invokespecial  #46; //Method java/lang/Integer."<init>":(I)V
  9:  astore_1
  10: new   #44; //class java/lang/Integer
  13: dup
  14: bipush 100
  16: invokespecial  #46; //Method java/lang/Integer."<init>":(I)V
  19: astore_2
  20: getstatic    #53; //Field java/lang/System.out:Ljava/io/PrintStream;
  23: new   #59; //class java/lang/StringBuilder
  26: dup
  27: ldc   #84; //String m21 result
  29: invokespecial  #63; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
  32: aload_1
  33: aload_2
  34: if_acmpne    41
  37: iconst_1
  38: goto  42
  41: iconst_0
  42: invokevirtual  #66; //Method java/lang/StringBuilder.append:(Z)Ljava/la
ng/StringBuilder;
  45: invokevirtual  #70; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
  48: invokevirtual  #74; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
  51: return
 
public void m22();
 Code:
  0:  new   #44; //class java/lang/Integer
  3:  dup
  4:  sipush 128
  7:  invokespecial  #46; //Method java/lang/Integer."<init>":(I)V
  10: astore_1
  11: new   #44; //class java/lang/Integer
  14: dup
  15: sipush 128
  18: invokespecial  #46; //Method java/lang/Integer."<init>":(I)V
  21: astore_2
  22: getstatic    #53; //Field java/lang/System.out:Ljava/io/PrintStream;
  25: new   #59; //class java/lang/StringBuilder
  28: dup
  29: ldc   #86; //String m22 result
  31: invokespecial  #63; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
  34: aload_1
  35: aload_2
  36: if_acmpne    43
  39: iconst_1
  40: goto  44
  43: iconst_0
  44: invokevirtual  #66; //Method java/lang/StringBuilder.append:(Z)Ljava/la
ng/StringBuilder;
  47: invokevirtual  #70; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
  50: invokevirtual  #74; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
  53: return
Copy after login

##m3

public class DataType {
  
  public static void main(String args[]) {
    DataType dt = new DataType();
    dt.m31();
    dt.m32();
  }
  
  public void m31() {
    Integer a = 100;
    Integer b = 100;
    System.out.println("m31 result " + (a == b));
  }
  
  public void m32() {
    Integer a = 128;
    Integer b = 128;
    System.out.println("m32 result " + (a == b));
  }
  
  
}
Copy after login

Print result

m31 result true
m32 result false
Copy after login

Why is the first one true and the second one false? Observe the data parsed by javap

javap parsed content

public void m31();
 Code:
  0:  bipush 100
  2:  invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  5:  astore_1
  6:  bipush 100
  8:  invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  11: astore_2
  12: getstatic    #53; //Field java/lang/System.out:Ljava/io/PrintStream;
  15: new   #59; //class java/lang/StringBuilder
  18: dup
  19: ldc   #88; //String m31 result
  21: invokespecial  #63; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
  24: aload_1
  25: aload_2
  26: if_acmpne    33
  29: iconst_1
  30: goto  34
  33: iconst_0
  34: invokevirtual  #66; //Method java/lang/StringBuilder.append:(Z)Ljava/la
ng/StringBuilder;
  37: invokevirtual  #70; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
  40: invokevirtual  #74; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
  43: return
 
public void m32();
 Code:
  0:  sipush 128
  3:  invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  6:  astore_1
  7:  sipush 128
  10: invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  13: astore_2
  14: getstatic    #53; //Field java/lang/System.out:Ljava/io/PrintStream;
  17: new   #59; //class java/lang/StringBuilder
  20: dup
  21: ldc   #90; //String m32 result
  23: invokespecial  #63; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
  26: aload_1
  27: aload_2
  28: if_acmpne    35
  31: iconst_1
  32: goto  36
  35: iconst_0
  36: invokevirtual  #66; //Method java/lang/StringBuilder.append:(Z)Ljava/la
ng/StringBuilder;
  39: invokevirtual  #70; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
  42: invokevirtual  #74; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
  45: return
Copy after login

m4

public class DataType {
  
  public static void main(String args[]) {
    DataType dt = new DataType();
    dt.m41();
    dt.m42();
  }
  
  
  public void m41() {
    Integer a = Integer.valueOf(100);
    Integer b = 100;
    System.out.println("m41 result " + (a == b));
  }
    
  public void m42() {
    Integer a = Integer.valueOf(128);
    Integer b = 128;
    System.out.println("m42 result " + (a == b));
  }
}
Copy after login

Print results

m41 result true
m42 result false
Copy after login

##javap analysis content

public void m41();
 Code:
  0:  bipush 100
  2:  invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  5:  astore_1
  6:  bipush 100
  8:  invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  11: astore_2
  12: getstatic    #53; //Field java/lang/System.out:Ljava/io/PrintStream;
  15: new   #59; //class java/lang/StringBuilder
  18: dup
  19: ldc   #92; //String m41 result
  21: invokespecial  #63; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
  24: aload_1
  25: aload_2
  26: if_acmpne    33
  29: iconst_1
  30: goto  34
  33: iconst_0
  34: invokevirtual  #66; //Method java/lang/StringBuilder.append:(Z)Ljava/la
ng/StringBuilder;
  37: invokevirtual  #70; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
  40: invokevirtual  #74; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
  43: return
 
public void m42();
 Code:
  0:  sipush 128
  3:  invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  6:  astore_1
  7:  sipush 128
  10: invokestatic  #49; //Method java/lang/Integer.valueOf:(I)Ljava/lang/In
teger;
  13: astore_2
  14: getstatic    #53; //Field java/lang/System.out:Ljava/io/PrintStream;
  17: new   #59; //class java/lang/StringBuilder
  20: dup
  21: ldc   #94; //String m42 result
  23: invokespecial  #63; //Method java/lang/StringBuilder."<init>":(Ljava/la
ng/String;)V
  26: aload_1
  27: aload_2
  28: if_acmpne    35
  31: iconst_1
  32: goto  36
  35: iconst_0
  36: invokevirtual  #66; //Method java/lang/StringBuilder.append:(Z)Ljava/la
ng/StringBuilder;
  39: invokevirtual  #70; //Method java/lang/StringBuilder.toString:()Ljava/l
ang/String;
  42: invokevirtual  #74; //Method java/io/PrintStream.println:(Ljava/lang/St
ring;)V
  45: return
 
}
Copy after login

##Analysis

javap is a tool that comes with Java. It can decompile and view the bytecode generated by the Java compiler (the above code only uses javap -c DataType). It is a good tool for analyzing code. How to use it specifically Please Google it

First look at m4, why does "true" appear in the running result? True means that a and b are the same object.

But object a is generated by calling Integer.valueOf(), and object b is generated by automatic boxing. Why is it the same object? Let’s take a look at the bytecode again. After all, Java programs are implemented by relying on the virtual machine to run the bytecode.

m41 This method only applies valueOf() once, but it appears twice in the bytecode, indicating that valueOf() is also called during autoboxing.

The following is the specific implementation of valueOf()

/**
 * Returns a <tt>Integer</tt> instance representing the specified
 * <tt>int</tt> value.
 * If a new <tt>Integer</tt> 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.
 *
 * @param i an <code>int</code> value.
 * @return a <tt>Integer</tt> instance representing <tt>i</tt>.
 * @since 1.5
 */
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
  return IntegerCache.cache[i + offset];
}
  return new Integer(i);
}
Copy after login

The number between [-128, 127], valueOf returns the cache object, so the two calls return the same object.

Thank you for reading, I hope it can help you, thank you for your support of this site!

For more Java boxing and unboxing detailed explanations and example code related articles, 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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

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

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

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

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

How can I implement functional programming techniques in Java? How can I implement functional programming techniques in Java? Mar 11, 2025 pm 05:51 PM

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

How to Share Data Between Steps in Cucumber How to Share Data Between Steps in Cucumber Mar 07, 2025 pm 05:55 PM

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive

See all articles