
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
method overloading and type promotion in Java
Method overloading helps to create multiple methods with the same name to perform similar operations in different ways ( using different types of parameters). Automatic Type promotion or conversion in Java is a process in which the compiler automatically promotes a small-sized data type to a larger-sized data type. It is not possible to convert a larger size to a smaller size automatically.
In method overloading, the overloaded method is called based on the arguments passed. Suppose we are calling an overloaded method, and the type of argument is not the exact match to the type defined in the method definition, depending on the type size of the given values, automatic type promotion may occur.
In this article, we are going to see different cases for automatic type promotion in method overloading.
Possible Type Conversion in Method Overloading
The different cases for automatic type promotion in method overloading is as follows:
- Passing arguments of same datatype
- Passing arguments of smaller datatype
- Passing arguments of larger datatype
Case 1: Passing Arguments of Same Datatype
If the datatype of arguments and parameters is an exact match, then the compiler will run the Java program without any issue.
Example
The following example is the implementation of the above discussed case-
public class Main { // Method with int parameter void show(int num) { System.out.println("Integer value: " + num); } // Overloaded method with two int parameters void show(int num1, int num2) { System.out.println("Two integer values: " + num1 + " and " + num2); } public static void main(String[] args) { Main obj = new Main(); // Calling methods with same datatypes obj.show(10); obj.show(15, 25); } }
Output of the above code is as follows:
Integer value: 10 Two integer values: 15 and 25
Case 2: Passing Arguments of Smaller Datatype
When you pass a smaller size datatype, Java automatically promotes it, if there is no exact match found for the given argument. Now, again compiler checks for the match in overloaded methods. If the match method is available, it will use that method.
Example
In this example, byte values are promoted to int. It demonstrates method overloading with type promotion, where smaller datatypes are converted to larger compatible ones to match method signatures.
public class Example { void show(int num) { System.out.println("Integer value: " + num); } void show(int num1, int num2) { System.out.println("Two integer values: " + num1 + " and " + num2); } public static void main(String[] args) { Example obj = new Example(); byte value = 5; obj.show(value); obj.show((byte) 10, (byte) 20); } }
On running, you will get the following result:
Integer value: 5 Two integer values: 10 and 20
Case 3: Passing Arguments of Larger Datatype
While resolving overloaded methods, if an exact match is not found, compiler does not immediately throw an error. Instead, it first converts the lower data type argument to the higher one and then checks if there is any matching method available or not.
In the case of larger datatype, we will get a compile-time error because automatic type promotion from a larger data type to a smaller one is not possible in method overloading.
Example
Let's see the practical implementation:
public class Example { void show(int num) { System.out.println("Integer value: " + num); } void show(int num1, int num2) { System.out.println("Two integer values: " + num1 + " and " + num2); } public static void main(String[] args) { Example obj = new Example(); obj.show(15.5); obj.show(15.5, 18.5); } }
When you run the program, you will encounter an error:
2 errors ERROR! error: compilation failed