Method Overloading with Autoboxing and Widening in Java

3.1

Prerequisite: Method Overloading, Autoboxing and Unboxing

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 GFG 
{
	
	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 GFG 
{
	
	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 refernce type formal argument
	public void method(Integer a)
	{
		
		System.out.println("Primitive type byte formal argument :" + a);
	}
	
}

class GFG 
{
	
	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 refernce type
// variables
import java.io.*;

public class Conversion 
{
	
	// overloaded method with refernce type
	// formal argument
	public void method(Object b)
	{
		
		// Object b is typecasted to Byte and then printed 
		Byte bt = (Byte) b;
		System.out.println("refernce type formal argument :" + bt);
	}
	
}

class GFG 
{
	
	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 GFG 
{
	
	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

This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.1 Average Difficulty : 3.1/5.0
Based on 6 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.