Autoboxing
Autoboxing is converts primitive data type into it's equivalent wrapper type class automatically in Java. E.g: int primitive type is converted to Integer Wrapper class in autoboxing.
Widening
Widening in Java can be defined as converting a variable of a particular type into wider type. E.g: short is converted int primitive type.
In method overloading,widening beats boxing whenever there is possibility of widening and boxing.
Program of Autoboxing where widening beats boxing:
class Boxing1
{
static void m(int i) //overloaded method,int primitive type
{
System.out.println("int");
}
static void m(Integer i) //overloaded method,Integer wrapper type
{
System.out.println("Integer");
}
public static void main(String args[])
{
short s=30;
m(s);
}
}
Output:
int
In above program when overloaded method m is called from main with short primitive type value as parameter in m then it is converted to wider type i.e int primitive type and thus m function with int primitive type as an argument is called.
0 Comment(s)