How does auto boxing/unboxing work in Java?
Asked Answered
A

4

24

Since JDK 5.0, auto boxing/unboxing was introduced in Java. The trick is simple and helpful, but when I started testing different conversions between wrapper classes and primitive types, I get really confused how the concept of auto boxing works in Java. For example:

Boxing

int intValue = 0;
Integer intObject = intValue;
byte byteValue = 0;
intObject = byteValue; // ==> Error

After trying different cases (short, long, float, double), the only case which is accepted by the compiler is when the type of the value on the right of affectation operator is int. When I looked inside the source of Integer.class I found that it implements only one constructor with int parameter.

So my conclusion is that the concept of auto boxing is based on constructor implemented in the wrapper class. I want to know if this conclusion is true or there is another concept used by auto boxing?

Unboxing

Integer intObject = new Integer(0);
byte byteValue = intObject; // ==> Error (the same Error with short)
int intValue = intObject; 
double doubleValue = intObject;

My conclusion about unboxing is that the wrapper class gives the value wrapped by the object in the corresponding type (Integer ==> int), then the compiler use the usual rules of converting primitive types (byte => short => int => long => float => double). I want to know if this conclusion is true or there is another concept used by auto unboxing?

Aguascalientes answered 25/3, 2014 at 23:41 Comment(4)
Have you read the Java Language specification sections on autoboxing? docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.7Laryngeal
byte byteValue = intObject; - you do realize that's an error regardless of whether intObject is an object or a primitive type, right?Skylar
I thinks the compiler use the method Integer.intValue() to unbox the wrapped value, is this correct?Aguascalientes
@LouisWasserman Thanks so much your link is really helpful, it answers my question.Aguascalientes
F
23

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.

Flashover answered 25/3, 2014 at 23:47 Comment(4)
In the fact i'm java beginner (i don't understand bytecode), so when i started reading a chapter of autoboxing it gives just some examples of limitations of auto boxing/unboxing and not all rules, then i tried to parse all cases, and want to confirm if my comprehension of this concept is corrected or not.Aguascalientes
@NarutoBijuMode I think your main misconception was about how the (un)boxing happens. As I pointed out in the answer, it just comes down to the invocation of two specific methods; valueOf() and intValue() (respectively).Flashover
Thanks for help :), i think i'm the stupid one who asked an explanation which i will not understand in this moment (i should learn more about java). Thanks.Aguascalientes
I thought implicit boxing/unboxing was expensive and never really recommended. I think Joshua Bloch might have said that? So is it now acceptable to use implicit conversion and not have to explicitly call valueOf() and intValue()?Slemmer
P
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.

Parterre answered 3/5, 2019 at 15:13 Comment(0)
B
1

Consider the following code as an example of auto un-boxing:

System.out.println('b'+ new Integer(63));

Here's a breakdown of how the above code is compiled:

Step 1: Object Integer 63 is instantiated and then AUTO-UNBOXED to int 63

new Integer(63)

Step 2: char 'b' is converted to numeric value i.e. 98

Step 3: the two values are added: 98+63

Step 4: Output is 161

Bellanca answered 16/11, 2016 at 11:8 Comment(0)
N
0

Auto boxing and auto unboxing

Auto boxing means when we try to assign a primitive data to a object type it automatically converts itself to the object type.that process called auto boxing.. and when a object type converts to the primitive type its called unboxing...try to understand it from following examples.

class Demo{
public static void main(String args[]){
    int x=100;

    //Integer iob=x; //Illegal jdk1.4
    Integer iob=Integer.valueOf(x); //Legal at JDK1.4 =>Boxing

    Integer iob2=x; //Legal JDK1.5 - Autoboxing
    System.out.println(iob2);
}

}

another example for auto boxing

class Demo{
public static void main(String args[]){
    Integer iob=new Integer(100);    
    int x;
    x=iob; //Legal => auto unboxing
    System.out.println(x);
}

}

example for auto unboxing

class Demo{
public static void main(String args[]){
    Integer iob=new Integer(100);
    int x=iob; //Auto unboxing ==>Assignment

}

}

thank you..

Nonresistance answered 29/6, 2016 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.