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???
- 35,468
- 12
- 66
- 99
- 69
- 1
- 2
- 10
-
They are two related but different concepts. In language form, one would be a subject and other a verb. – Kayaman Dec 09 '15 at 07:47
-
Possible duplicate of [Java: What's the difference between autoboxing and casting?](http://stackoverflow.com/questions/501653/java-whats-the-difference-between-autoboxing-and-casting) – Dennis Kriechel Dec 09 '15 at 07:48
-
For java - *Autoboxing* and *Unboxing* come into picture because we have wrappers for primitives. Without wrapper classes, what would primitives box to? – TheLostMind Dec 09 '15 at 07:48
-
@Kayaman would you mind explaining in English language? – abhishek shivdekar Dec 09 '15 at 07:52
-
@VinodMadyalkar thx! can i get more clear idea from you? – abhishek shivdekar Dec 09 '15 at 07:56
-
@abhishekshivdekar I did. I even made an analogy of the English language to explain it. – Kayaman Dec 09 '15 at 07:59
-
@abhishekshivdekar - Can't do it better that *andreas* :) – TheLostMind Dec 09 '15 at 08:43
-
@VinodMadyalkar No problem sir , still thx! – abhishek shivdekar Dec 09 '15 at 09:25
3 Answers
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.
- 147,606
- 10
- 133
- 220
Autoboxingis 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 calledunboxing.
Here is the simplest example of autoboxing:
Character ch = 'a';
The
wrapperclasses 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");
- 8,257
- 4
- 21
- 35
-
Thx sir confusion cleared ! nice explaination and thx for answering it in simple ways than other java gods here were answering – abhishek shivdekar Dec 09 '15 at 08:05
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.
- 1,418
- 1
- 9
- 18