Introduction
Java is an object-oriented class-based programming language used in Front-end and Back-end. It is used to develop mobile applications, web applications, games, GUI applications, etc. It contains classes, methods, and objects encapsulated as a unit. Java is a platform-independent programming language. It can be used anywhere independent of the systems because it is compiled into bytecodes that can be run on any Java Virtual Machine(JVM).

Recommended topic - Iteration Statements in Java, Duck Number in Java
What is Method overloading?
A program should be efficient and less complex. Suppose you need to perform any computation or operation more than once in a program with different inputs. It is difficult to write different methods multiple times with different parameters as it might confuse the other programmers. So, to avoid ambiguity, we use method overloading with the same method names and different parameters. It lets us know that the methods are the same and have the same functionality.
The methods inside Java contain the logic/ functionality to execute the program. There can be multiple methods inside a single class with any return types like int, float, void, String, etc. The technique of re-using the methods with the same name but different parameters is called Method overloading. The method's return type can be either be different or the same.
The method overloading can be done in two ways.
- By changing the data type of parameters.
- By changing the number of parameters.
- By changing the sequence/order of parameters
Also see, Swap Function in Java
By changing the data type of parameters
We can overload any method by changing the data types of the parameters we send while invoking the method. Let’s consider an example with the same methods now.
class MethodOverloadingExample {
static int addition(int x, int y) {
return x + y;
}
static double addition(int x, double y) {
return x + y;
}
public static void main(String[] args) {
int a = addition(8, 8);
double b = addition(4, 5.2);
System.out.println("addition of integers: " + a);
System.out.println("addition of double values: " + b);
}
}
We created a class MethodOverloadingExample in the above example containing the method addition with different return types. We invoked the method addition from the main method with different parameters of int and double.
Output
addition of integers: 16
addition of integers and double values: 9.2
You can also find the output of this java compiler code here.
By changing the number of parameters
We can overload any method by changing the number of parameters we send while invoking the method. Let’s learn overloading with changing the number of parameters now.
class MethodOverloadingExample {
static int addition(int x, int y, int z) {
return x + y + z;
}
static int addition(int x, int y) {
return x + y;
}
public static void main(String[] args) {
int a = addition(8, 2, 5);
double b = addition(4, 5);
System.out.println("addition of 3 integers: " + a);
System.out.println("addition of 2 integers: " + b);
}
}
We created a class MethodOverloadingExample in the above example that contains the method addition with the same return types but different number of parameters. We invoked the method addition from the main method with two and three parameters, respectively.
Output
addition of 3 integers: 15
addition of 2 integers: 9
By changing the sequence/order of parameters
We can overload any method by changing the data types of the parameters we send while invoking the method.
class MethodOverloadingExample {
static double addition(double x, int y) {
return x + y;
}
static double addition(int x, double y) {
return x + y;
}
public static void main(String[] args) {
int a = addition(5.2, 8);
double b = addition(4, 5.2);
System.out.println("addition of double and int: " + a);
System.out.println("addition of int and double: " + b);
}
}
We created a class MethodOverloadingExample in the above example containing the method addition with the same return types, but the order of parameters is different. We invoked the method addition from the main method with different parameters of int and double.
Output
addition of double and int: 13.2
addition of double and int: 9.2
Must Read Conditional Statements in Java and Hashcode Method in Java.