Method Overloading with Primitive Data Types and Their Wrappers in Java


Write a Java program to demonstrate method overloading with primitive data types and their wrappers

This Java program demonstrates method overloading based on different data types, including primitive types and their corresponding wrapper classes.

  • The main method creates an instance of the DataTypeOverloading class.
  • It then defines a primitive int (primitiveInt) and a wrapper Integer (wrapperInt) with the same value.
  • The display methods are called with these values as arguments.
  • The Java compiler determines which method to call based on the data types of the arguments passed.
  • For the first call, data.display(primitiveInt), the method display(int num) is invoked.
  • For the second call, data.display(wrapperInt), the method display(Integer num) is invoked.
  • The appropriate method implementation is executed, and the corresponding message is printed to the console.

Source Code

class DataTypeOverloading
{
	void display(int num)
	{
		System.out.println("Primitive int : " + num);
	}
 
	void display(Integer num)
	{
		System.out.println("Wrapper Integer : " + num);
	}
 
	public static void main(String[] args)
	{
		DataTypeOverloading data = new DataTypeOverloading();
		int primitiveInt = 47;
		Integer wrapperInt = 47;
		data.display(primitiveInt);
		data.display(wrapperInt);
	}
}

Output

Primitive int : 47
Wrapper Integer : 47

Example Programs