1

A Wrapper Class is used to convert primitive into object and object into primitive. Similarly by using Autoboxing and Unboxing we can do the same then what is the difference in these two: 1-Concept wise 2-Code wise???

TheLostMind
  • 35,468
  • 12
  • 66
  • 99
abhishek shivdekar
  • 69
  • 1
  • 2
  • 10

3 Answers3

7

Auto-boxing and auto-unboxing is just the compiler silently helping you create and use primitive wrapper objects.

For example, the int primitive type has wrapper class called Integer. You wrap and unwrap as follows:

int myInt = 7;

// Wrap the primitive value
Integer myWrappedInt = Integer.valueOf(myInt);

// Unwrap the value
int myOtherInt = myWrappedInt.intValue();

With auto-boxing and auto-unboxing, you don't have to do all that boiler-plate stuff:

int myInt = 7;

// Wrap the primitive value
Integer myWrappedInt = myInt; // Compiler auto-boxes

// Unwrap the value
int myOtherInt = myWrappedInt; // Compiler auto-unboxes

It's just a syntactic sugar, handled by the compiler. The generated byte code is the same.

Andreas
  • 147,606
  • 10
  • 133
  • 220
2

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 double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

Here is the simplest example of autoboxing:

 Character ch = 'a';

The wrapper classes provide a mechanism to "wrap" primitive values in an object so that the primitives can be included in activities only for objects, like being added to Collections. There is a wrapper class for every primitive in Java. The wrapper class for int is Integer and the class for float is Float and so on. Wrapper classes also provide many utility functions for primitives like Integer.parseInt().

Three most common approaches for creating wrapper objects are:

1. Using constructor as

 Integer i1 = new Integer(42);

All of the wrapper classes except Character provide two constructors: one that takes a primitive type and one that takes a String representation of the primitive type: new Integer(42) or new Integer("42"). The Character class provides only one constructor, which takes a char as an argument: new Character('c').

2. Using valueOf() as

 Float f2 = Float.valueOf("3.14f");

3. Directly assigning a primitive to a wrapper reference, in which case java will automatically create an object for you, which is called as Autoboxing as

 Long weight = 1200L;

There are many utility functions for the wrapper class, mainly for conversions like:

  double d4 = Double.parseDouble("3.14");

  Double d5 = Double.valueOf("3.14");
Abdelhak
  • 8,257
  • 4
  • 21
  • 35
0

Wrapper classes in java provides a mechanism to convert primitive into object and object into primitive. Whereas automatic boxing and unboxing allows you to do that conversion automatically. Autoboxing and auto unboxing are legal in java since Java 5.

public class Main {

    public static void main(String[] args) {
        int x=100;
        Integer iob;
        iob=x;//illegal upto JDK1.4
        iob= Integer.valueOf(x); //legal=> Boxing,Wrapping
        iob=x; //Legal since JDK1.5=> Auto Boxing

    }
}

Here x is a primitive variable, iob is a reference variable. So only an address can be assigned into this iob reference. iob=Integer.valueOf(x) will convert primitive integer into integer object. This conversion can be implied as wrapping. iob=x will do the same thing. It also saves a lot of coding.

public class Main {

    public static void main(String[] args) {
        Integer iob= new Integer(100);
        int x;

        x=iob;//illegal upto JDK1.4
        x=iob.intValue(); //unboxing,unwrapping

        x=iob; //legal since JDK1.5=> auto unboxing 

    }
}

Here iob.intValue() will take the value of integer object and it will assign that value into x. x=iob does the same thing except you don't need to write conversion code.

Sahan Amarsha
  • 1,418
  • 1
  • 9
  • 18