Course Curriculum

Method Overloading with Autoboxing and Widening in Java

Method Overloading with Autoboxing and Widening in Java

In Java there are two type of variables: Primitive type and Reference type. Conversion of primitive type to its corresponding wrapper Object is called Autoboxing and Wrapper Object to its corresponding primitive type is known as Unboxing.

Method Overloading with Autoboxing

In method overloading, you may come across a situation where a signature takes reference type or a primitive type as a formal argument. The compiler first searches a method with parameter(s) of the same data type(s). If you are using wrapper class Object as an actual argument and compiler does not find the method with parameter(s) of the same reference type (i.e. class or interface type), then it starts searching a method with parameter(s) having the corresponding primitive data type.

// Java program to illustrate
// Autoboxing
// while resolving data type as:
// a)reference b)primitive
import java.io.*;

public class Conversion
{

// 1.overloaded method with primitive formal argument
public void method(int i)
{

System.out.println("Primitive type int formal argument :" + i);
}

// overloaded method with reference formal argument
public void method(Integer i)
{

System.out.println("Reference type Integer formal argument :" + i);
}

// 2. overloaded method primitive formal argument
// and to be invoked for wrapper Object as overloaded method
// with wrapper object of same(Long) type as an argument is not
// available.
public void method(long i)
{

System.out.println("Primitive type long formal argument :" + i);
}
}

class P_AI
{

public static void main (String[] args)
{
Conversion c = new Conversion();

// invoking the method with different signature.
c.method(10);
c.method(new Integer(15));
c.method(new Long(100));

// Using short will give, argument mismatch;
// possible lossy conversion from int to short
// c.method(new Short(15));

}
}
Output:

Primitive type int formal argument :10
Reference type Integer formal argument :15
Primitive type long formal argument :100

Method Overloading with Widening

If compiler fails to find any method corresponding to autoboxing, then it starts searching a method parameter(s) of the widened primitive data type.
In the example below, we are invoking overloaded method with primitive(int) formal argument that has the same data type as actual argument’s data type. We are invoking another method with argument of Long wrapper Object. Compiler starts searching for the method having the same reference type (Long wrapper class). Since there is no method having with parameter of Long wrapper class. So, It searches for method which can accept the parameter bigger than long primitive data type as an argument. In this case, it finds a method with float primitive data type and invokes it.

// Java program to illustrate method
// overloading
// in case of widening
import java.io.*;

public class Conversion
{
// overloaded method
public void method(int i)
{
System.out.println("Primitive type int formal argument :" + i);
}

// overloaded method primitive formal argument
// and to be invoked for wrapper Object as

public void method(float i)
{

System.out.println("Primitive type float formal argument :" + i);
}
}

class P_AI
{

public static void main (String[] args)
{

Conversion c = new Conversion();

// invoking the method with signature
// has widened data type
c.method(10);
c.method(new Long(100));
}
}
Output:

Primitive type int formal argument :10
Primitive type float formal argument :100.0

Method Overloading with Widening and Boxing Together

What happens if widening and boxing happen together? What method invocation will compiler be able to do?
Widening of primitive type has taken priority over boxing and var-args. But widening and boxing of primitive type can not work together.

// Java program to illustrate method
// overloading for widening
// and autoboxing together
import java.io.*;

public class Conversion
{
// overloaded method with reference type formal argument
public void method(Integer a)
{

System.out.println("Primitive type byte formal argument :" + a);
}

}

class P_AI
{

public static void main (String[] args)
{

Conversion c = new Conversion();

// invoking the method
byte val = 5;
c.method(val);
}
}
Output:

25: error: incompatible types: byte cannot be converted to Integer
c.method(val);
^
Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
But boxing followed by widening is acceptable if this is passed to a reference of type Object. See the following example for this.

// Java program to illustrate
// autoboxing followed by
// widening in reference type
// variables
import java.io.*;

public class Conversion
{

// overloaded method with reference type
// formal argument
public void method(Object b)
{

// Object b is typecasted to Byte and then printed
Byte bt = (Byte) b;
System.out.println("reference type formal argument :" + bt);
}

}

class P_AI
{

public static void main (String[] args)
{

Conversion c = new Conversion();

byte val = 5;

// b is first widened to Byte
// and then Byte is passed to Object.
c.method(val);
}
}
Output:

Primitive type byte formal argument :5

Method Overloading with Var-args argument

Widening of primitive type gets more priority over var-args.
Example:

// Java program to illustrate
// method overloading for var-args
// and widening concept together
import java.io.*;

public class Conversion
{
// overloaded method primitive(byte) var-args formal argument
public void method(byte... a)
{
System.out.println("Primitive type byte formal argument :" + a);
}

// overloaded method primitive(int) formal arguments
public void method(long a, long b)
{
System.out.println("Widening type long formal argument :" + a);
}
}

class P_AI
{

public static void main (String[] args)
{
Conversion c = new Conversion();

// invokes the method having widening
// primitive type parameters.
byte val = 5;
c.method(val,val);
}
}
Output:

Widening type long formal argument :5

(Next Lesson) How to start learning Java