Reputation: 13423
Are autoboxing and unboxing actually fancy terms for operator overloading? What happens when we say Integer i = 10;
?
Upvotes: 1
Views: 1754
Reputation: 115
Autoboxing: It is the process of converting premitive data type to reference datatype(relative wrapper class). ex: int x=5;
Integer ob=new Integer(x);
Here 'x'(int) is premitive and converted as reference datatype(Integer).
we can do autoboxing with out creating object.
Integer ob=x;
Here implicitly compiler creates an object like as created above.
Autounboxing:
The process of converting reference datatype to premitive datatype.
int x=5; Integer ob=x;//autoboxing
int x=ob.intValue();//autounboxing
We can write with out using method intValue() in the step of autounboxing.
int x=ob;//autounboxing.
Here compiler implicitly call intValue() method.
This feature is introduced in jdk1.5 in 2004.
Upvotes: 0
Reputation: 11
Autoboxing and unboxing is introduced in java with Java 5. Lets consider one example to understand these terms. Check the below code and think if it will compile or not...
int sum(int a, int b)
{
return a+b;
}
Integer I =sum(3,4);
In the above example if you will try to compile it with java 1.4 or below you will see compile error. Compiler will complain about return type of method "sum" and the type of variable "I" as method is returning primitive datatype where as Integer is a wrapper class for int. But the same code will compile and run fine with java 5 because of the functionality called autoboxing. In autoboxing compiler converts primitive datatype to corresponding object wrapper class eg. int to Integer, char to Character etc. The java compiler applies autoboxing when a primitive type value is Assigned to a variable of the corresponding wrapper class. Passed as a parameter to a method which is expecting input parameter as corresponding wrapper class.
Good use of autoboxing/unboxing is there with Generics where we define a collection of certain type(some wrapper class object) and add primitive type data to it. Refer below example
List<Integer> lst=new ArrayList();
lst.add(5);
On similar path converting wrapper type object to its corresponding primitive type is known as unboxing in java.
You can find details at http://javatechnologyhelper.blogspot.com/2014/07/autoboxing-unboxing-in-java.html
Upvotes: 1
Reputation: 1463
Autoboxing / unboxing is a compiler feature, so you can't implement it yourself. Operator overloading is not possible in Java.
Here is a good example (similar to what you asked), about what happens with primitive type boxing: http://www.leepoint.net/notes-java/data/basic_types/autoboxing.html
Upvotes: 1
Reputation: 12545
No, it's not operator overloading. Java doesn't provide any mechanism for operator overloading.
Integer i = 10;
Is like saying:
Integer i = Integer.valueOf(10);
Which isn't overloading =
at all.
Upvotes: 6
Reputation: 3205
It's not operator overloading but for all practical purposes its the same thing as a combined operator/method overload. As pointed out in other answers the generated code lives in the caller rather than the callee as it would do if the method were truly overloaded.
Upvotes: 0
Reputation: 147164
Boxing and unboxing are conversions, much the same as converting int
to a float
. It's about adjusting types before applying operations rather than operators.
Upvotes: 0
Reputation: 234807
Like everyone else is saying, auto(un)boxing is not operator overloading. Java's operator overloading is restricted to what's defined in the language. So, for instance, there is an =
operator for every primitive type (including object reference); a logical |
and an int (bit-wise) |
; etc.
Upvotes: 0
Reputation: 95519
No, autoboxing and unboxing are simply "syntactic sugar". It means that there is additional computation implied (that needs to be emitted in byte code), but which the compiler handily hides for you.
Upvotes: 2
Reputation: 80186
internally compiler generates Integer i=Integer.valueOf(10);
Upvotes: 3