here's a resource that helped me in learning java geeksForGeeks . After struggling with java for work for several months, I wish the following textbook was found earlier: Java How To Program, Early Objects My friend who actually studied computer science slapped me in the face with it. It's way better than OCA test prep books to achieve understanding Answer from Cesar-Oswaldo on reddit.com
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › autoboxing.html
Autoboxing and Unboxing (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
import java.util.ArrayList; import java.util.List; public class Unboxing { public static void main(String[] args) { Integer i = new Integer(-8); // 1. Unboxing through method invocation int absVal = absoluteValue(i); System.out.println("absolute value of " + i + " = " + absVal); List<Double> ld = new ArrayList<>(); ld.add(3.1416); // Π is autoboxed through method invocation.
🌐
GeeksforGeeks
geeksforgeeks.org › java › autoboxing-unboxing-java
Autoboxing and Unboxing in Java - GeeksforGeeks
It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example, conversion of Integer to int, Long to long, Double to double, etc.
Published   August 2, 2025
Discussions

Why do we use autoboxing and unboxing in Java? - Stack Overflow
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a d... More on stackoverflow.com
🌐 stackoverflow.com
autoboxing - How does auto boxing/unboxing work in Java? - Stack Overflow
You can read about boxing and unboxing conversions in JLS §5.1.7 and JLS §5.1.8, respectively. ... Sign up to request clarification or add additional context in comments. ... In the fact i'm java beginner (i don't understand bytecode), so when i started reading a chapter of autoboxing it gives ... More on stackoverflow.com
🌐 stackoverflow.com
Friendly reminder about Integer, int, nulls and autoboxing/unboxing

I ignore autoboxing warnings, until the other day..

ResultSet RS = doSomeDatabaseQuery();
Double foo = RS.getDouble("someDoubleField");

Silly me, thinking "getDouble()", which returns a field from a database which can quite plausibly be NULL, would return a Double. No, it returns a double. If the value in the db was NULL, it returns a 0.

What you actually have to do is:

ResultSet RS = doSomeDatabaseQuery();
double foo = RS.getDouble("someDoubleField");
Double bar = RS.wasNull() ? null : Double.valueOf(foo);

Because I had autoboxing warnings turned off, we didn't notice this bug. Let this be a lesson! Don't ignore warnings!

More on reddit.com
🌐 r/java
19
14
October 3, 2013
Java autoboxing and the ternary operator
I would argue that this is perfectly "correct" behavior simply out of consistency: Object o1 = 1 + 2.0; System.out.println(o1.getClass()); // "class java.lang.Double" Object o2 = true ? 1 : 2.0; System.out.println(o2.getClass()); // "class java.lang.Double" Object o3 = true ? new Integer(1) : new Double(2.0); System.out.println(o3.getClass()); // "class java.lang.Double" It's important to remember that operators in Java are effectively primitive and always have primitive operands. The ternary operator is really no exception. It also helps to think of a reference as just another primitive type; only the reference target (the "value") is non-primitive. Also, all the Java primitives have associated wrapper classes, and a wrapper (by definition) should convert to a primitive and back with no loss of information: Integer obj; int i; obj = i = new Integer(1); System.out.println(obj == i); // true System.out.println(obj.equals(i)); // true Since Java does not support operator overloading, the == in the previous example (being a primitive operator) must "unbox" obj, while the .equals() (being a method invocation) must "box" i. If you understand that objects of a primitive type's wrapper class will always be unboxed when using primitive operators (+, -, ?:, etc.), the whole process makes a lot more sense. Another way to think of it is to pretend that any given assignment is the result of a method invocation. For example, the below would simply not work as the method must return Double or any common super-type (like Number or Object): Integer method() { return(true ? new Integer(0) : new Double(0)); // fails! } More on reddit.com
🌐 r/programming
38
13
January 20, 2011
🌐
Medium
medium.com › @AlexanderObregon › how-javas-autoboxing-and-unboxing-work-78d9ebcdf4d1
How Java’s Autoboxing and Unboxing Work | Medium
February 15, 2025 - Autoboxing happens when a primitive value is stored in its wrapper class, while unboxing is the reverse — converting a wrapper object back into a primitive. These conversions happen behind the scenes, so you don’t have to write any explicit ...
Top answer
1 of 10
194

Some context is required to fully understand the main reason behind this.

Primitives versus classes

Primitive variables in Java contain values (an integer, a double-precision floating point binary number, etc). Because these values may have different lengths, the variables containing them may also have different lengths (consider float versus double).

On the other hand, class variables contain references to instances. References are typically implemented as pointers (or something very similar to pointers) in many languages. These things typically have the same size, regardless of the sizes of the instances they refer to (Object, String, Integer, etc).

This property of class variables makes the references they contain interchangeable (to an extent). This allows us to do what we call substitution: broadly speaking, to use an instance of a particular type as an instance of another, related type (use a String as an Object, for example).

Primitive variables aren't interchangeable in the same way, neither with each other, nor with Object. The most obvious reason for this (but not the only reason) is their size difference. This makes primitive types inconvenient in this respect, but we still need them in the language (for reasons that mainly boil down to performance).

Generics and type erasure

Generic types are types with one or more type parameters (the exact number is called generic arity). For example, the generic type definition List<T> has a type parameter T, which can be Object (producing a concrete type List<Object>), String (List<String>), Integer (List<Integer>) and so on.

Generic types are a lot more complicated than non-generic ones. When they were introduced to Java (after its initial release), in order to avoid making radical changes to the JVM and possibly breaking compatibility with older binaries, the creators of Java decided to implement generic types in the least invasive way: all concrete types of List<T> are, in fact, compiled to (the binary equivalent of) List<Object> (for other types, the bound may be something other than Object, but you get the point). Generic arity and type parameter information are lost in this process, which is why we call it type erasure.

Putting the two together

Now the problem is the combination of the above realities: if List<T> becomes List<Object> in all cases, then T must always be a type that can be directly assigned to Object. Anything else can't be allowed. Since, as we said before, int, float and double aren't interchangeable with Object, there can't be a List<int>, List<float> or List<double> (unless a significantly more complicated implementation of generics existed in the JVM).

But Java offers types like Integer, Float and Double which wrap these primitives in class instances, making them effectively substitutable as Object, thus allowing generic types to indirectly work with the primitives as well (because you can have List<Integer>, List<Float>, List<Double> and so on).

The process of creating an Integer from an int, a Float from a float and so on, is called boxing. The reverse is called unboxing. Because having to box primitives every time you want to use them as Object is inconvenient, there are cases where the language does this automatically - that's called autoboxing.

2 of 10
24

Auto Boxing is used to convert primitive data types to their wrapper class objects. Wrapper class provide a wide range of function to be performed on the primitive types. The most common example is :

int a = 56;
Integer i = a; // Auto Boxing

It is needed because of programmers easy to be able to directly write code and JVM will take care of the Boxing and Unboxing.

Auto Boxing also comes in handy when we are working with java.util.Collection types. When we want to create a Collection of primitive types we cannot directly create a Collection of a primitive type , we can create Collection only of Objects. For Example :

ArrayList<int> al = new ArrayList<int>(); // not supported 

ArrayList<Integer> al = new ArrayList<Integer>(); // supported 
al.add(45); //auto Boxing 

Wrapper Classes

Each of Java's 8 primitive type (byte,short,int,float,char,double,boolean,long) hava a seperate Wrapper class Associated with them. These Wrapper class have predefined methods for preforming useful operations on primitive data types.

Use of Wrapper Classes

String s = "45";
int a = Integer.parseInt(s); // sets the value of a to 45.

There are many useful functions that Wrapper classes provide. Check out the java docs here

Unboxing is opposite of Auto Boxing where we convert the wrapper class object back to its primitive type. This is done automatically by JVM so that we can use a the wrapper classes for certain operation and then convert them back to primitive types as primitives result int faster processing. For Example :

Integer s = 45;
int a = s; auto UnBoxing;

In case of Collections which work with objects only auto unboxing is used. Here's how :

ArrayList<Integer> al = new ArrayList<Integer>();
al.add(45);

int a = al.get(0); // returns the object of Integer . Automatically Unboxed . 
🌐
Tutorialspoint
tutorialspoint.com › java › java-autoboxing-unboxing.htm
Java - Autoboxing and Unboxing
For example when an Integer object is passed to a method as argument but the method called is expecting an int variable, then compiler automatically converts the Integer object to int value and then pass it to the mathod called. Similarly Java compilier unboxes the wrapper value if it is assigned to a primitive variable.
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › java numbers › autoboxing and unboxing in java
Autoboxing and Unboxing in Java
In line 5, we assign y, which is an Integer object, to the primitive x. As you can see, we don't have to take any additional steps: the compiler knows that int and Integer are, essentially, the same thing. That's unboxing. Something similar is happening with autoboxing on line 6: the primitive value (x * 123) is easily assigned to object y.
Published   August 1, 2024
🌐
Medium
medium.com › @idiotN › autoboxing-and-unboxing-in-java-a-beginner-friendly-guide-a639d7bc6e65
what is autoboxing and unboxing in Java? | by idiot | Medium
November 4, 2025 - When preparing for Java interviews, you might encounter questions about autoboxing and unboxing. These concepts are fundamental to understanding how Java handles conversions between primitive types (like int, double) and their corresponding wrapper classes (like Integer, Double).
🌐
Hero Vired
herovired.com › learning-hub › topics › autoboxing-and-unboxing-in-java
Difference between Autoboxing and Unboxing in Java
January 20, 2025 - The opposite of autoboxing is unboxing. It entails automatically transforming a wrapper-type object into the matching primitive type. This usually occurs when a primitive type is anticipated in a scenario where a wrapper object is utilised. The Java compiler applies unboxing when an object ...
Top answer
1 of 4
24

When in doubt, check the bytecode:

Integer n = 42;

becomes:

0: bipush        42
2: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1      

So in actuality, valueOf() is used as opposed to the constructor (and the same goes for the other wrapper classes). This is beneficial since it allows for caching, and doesn't force the creation of a new object on each boxing operation.

The reverse is the following:

int n = Integer.valueOf(42);

which becomes:

0: bipush        42
2: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: invokevirtual #22                 // Method java/lang/Integer.intValue:()I
8: istore_1      

i.e. intValue() is used (again, it's analogous for the other wrapper types as well). This is really all auto(un)boxing boils down to.

You can read about boxing and unboxing conversions in JLS §5.1.7 and JLS §5.1.8, respectively.

2 of 4
7

This confusion can be cleared by using a switch of javac -XD-printflat which is very helpful in cases such as this one. So to unravel the mystery of boxing and unboxing you can write a simple program like following :

import java.util.*;

public class Boxing{
  public static void main(String[] args){
    Double d1 = 10.123;
    Float  f1 = 12.12f;
    Long   l1 = 1234L;
    Integer i1 = 55555;
    Short   s1 = 2345;
    Byte    b1 = 89;

    double d2 = d1;
    float  f2 = f1;
    long   l2 = l1;
    int    i2 = i1;
    short  s2 = s1;
    byte   b2 = b1;
  }
} 

and now we compile the above file as:

javac -XD-printflat -d src/ Boxing.java

output of this command is a java file with all the syntactic sugar (Generic types, enhanced for loop and in this case boxing-unboxing etc) removed. following is the output

import java.util.*;

public class Boxing {

    public Boxing() {
        super();
    }

    public static void main(String[] args) {
        Double d1 = Double.valueOf(10.123);
        Float f1 = Float.valueOf(12.12F);
        Long l1 = Long.valueOf(1234L);
        Integer i1 = Integer.valueOf(55555);
        Short s1 = Short.valueOf(2345);
        Byte b1 = Byte.valueOf(89);
        double d2 = d1.doubleValue();
        float f2 = f1.floatValue();
        long l2 = l1.longValue();
        int i2 = i1.intValue();
        short s2 = s1.shortValue();
        byte b2 = b1.byteValue();
    }
}

this is how java does boxing unboxing. using valueOf and ***Value methods.

🌐
GeeksforGeeks
geeksforgeeks.org › java › wrapper-classes-java
Wrapper Classes in Java - GeeksforGeeks
Wrapper classes provide utility methods such as compareTo(), equals(), and toString(). The automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing.
Published   April 6, 2026
🌐
DEV Community
dev.to › devnenyasha › understanding-wrapper-classes-autoboxing-and-unboxing-in-java-38eg
Understanding Wrapper Classes, Autoboxing, and Unboxing in Java - DEV Community
October 1, 2024 - Autoboxing and unboxing reduce boilerplate code by allowing automatic conversion between primitive types and their corresponding wrapper classes. Proper handling of null and exception scenarios ensures more robust code and avoid NullPointerException.
🌐
Dev.java
dev.java › learn › numbers-strings › autoboxing
Autoboxing and Unboxing - Dev.java
import java.util.ArrayList; import java.util.List; public class Unboxing { public static void main(String[] args) { Integer i = Integer.valueOf(-8); // 1. Unboxing through method invocation int absVal = absoluteValue(i); IO.println("absolute value of " + i + " = " + absVal); List<Double> doubles = new ArrayList<>(); doubles.add(3.1416); // Π is autoboxed through method invocation.
🌐
Scaler
scaler.com › home › topics › autoboxing and unboxing in java
Autoboxing and Unboxing in Java - Scaler Topics
July 27, 2023 - For example, it automatically converts from int to int, double to double, etc. Unboxing is the process of automatically converting objects of wrapper classes to their corresponding primitives, for as Integer to int.
🌐
LinkedIn
linkedin.com › all › javase
What are the benefits and drawbacks of using autoboxing and unboxing in Java?
March 15, 2024 - Autoboxing and unboxing in Java ... wrapper classes (Integer, Double). Autoboxing automatically converts primitives to wrapper objects when needed, while unboxing does the reverse....
🌐
Programiz
programiz.com › java-programming › autoboxing-unboxing
Java autoboxing and unboxing
import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); //autoboxing list.add(5); list.add(6); System.out.println("ArrayList: " + list); // unboxing int a = list.get(0); System.out.println("Value at index 0: " + a); } }
🌐
Just Academy
justacademy.co › blog-detail › difference-between-autoboxing-and-unboxing-in-java
Difference Between Autoboxing And Unboxing In Java
Autoboxing is the automatic conversion of a primitive data type to its corresponding wrapper class object, and unboxing is the automatic conversion of a wrapper class object back to its primitive data type in Java. Autoboxing simplifies the process of using primitive types as objects, while unboxing typically occurs when a method or operation requires a primitive type rather than a wrapper object...
🌐
Gopi Gorantala
ggorantala.dev › auto-boxing-and-auto-unboxing-in-java
Java Autoboxing and Unboxing
October 4, 2023 - This allows the conversion between primitive types and their corresponding wrapper classes to happen automatically when needed. The process of converting a primitive type to its corresponding wrapper class automatically.
🌐
Quora
quora.com › What-are-autoboxing-and-unboxing-in-Java
What are autoboxing and unboxing in Java? - Quora
Answer (1 of 3): Autoboxing: Converting a primitive value into an object of the corresponding wrapper class is called autoboxing. For example, converting int to Integer class. The Java compiler applies autoboxing when a primitive value is: * Passed as a parameter to a method that expects an obj...
🌐
guvi.in
studytonight.com › java › autoboxing-unboxing-java.php
Autoboxing and Unboxing in Java
Learn autoboxing and unboxing in Java, including automatic conversion between primitive types and wrapper classes, practical examples, benefits, and real-world usage i...