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.
varargs
varangs is variable arguments. varangs as name suggest allows a method to have 0 or mutiple argument.
Syntax of varargs:
return_type method_name(data_type... variableName){}
Proram of Autoboxing where widening beats varargs:
class Boxing1
{
static void m(int i1, int i2) //overloaded method with int primitive type
{
System.out.println("int int");
}
static void m(Integer... i) overloaded method with varangs
{
System.out.println("Integer...");
}
public static void main(String args[])
{
short s1=30,s2=40;
m(s1,s2);
}
}
Output:
int int
In above program from main when overloaded method m is called with two short values as parameter then function m with int primitive type is called because short variables type is automatically converted to wider type i.e int. Therefore widening beats varargs which was parameter of overloaded method m in which 0 or multiple Integer wrapper types can be passed.
0 Comment(s)