In this tutorial, we will learn how to implements methods or functions in Java.
In Java, a method is a collection of statements designed to perform specific tasks and, optionally, return a result to the caller. A Java method can also execute a task without returning any value. Methods enable code reuse by eliminating the need to retype code. Importantly, in Java, every method must belong to a class.
User-defined Methods
: Custom methods created by the programmer based on specific requirements.Standard Library Methods
: Built-in methods provided by Java, ready for immediate use.The basic syntax for a method declaration is:
returnType methodName() {
// method body
}
{ }
that defines the task to be performed.int addNumbers() {
// code
}
In this example, addNumbers()
is the method name, and its return type is int
.
modifier static returnType methodName(parameter1, parameter2, ...) {
// method body
}
The sqrt()
method in the Math class is static, so it can be accessed as Math.sqrt()
without creating an instance of Math.
To use a method, call it by name followed by parentheses.
class Main {
public int addNumbers(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int num1 = 25, num2 = 15;
Main obj = new Main(); // Create object of Main class
int result = obj.addNumbers(num1, num2);
System.out.println("Sum is: " + result);
}
}
Sum is: 40
Here, the addNumbers()
method takes two parameters and returns their sum. Since it’s not static, it requires an object to be called.
A method may or may not return a value. The return statement is used to return a value.
public static int square(int num) {
return num * num;
}
// void keyword is used as function does not return any value
public void printSquare(int num) {
System.out.println(num * num);
}
Methods can accept parameters, which are values passed to the method.
int addNumbers(int a, int b)
int addNumbers()
When calling a method with parameters, you must provide values for each parameter.
class Main {
public void display1() {
System.out.println("Method without parameter");
}
public void display2(int a) {
System.out.println("Method with a single parameter: " + a);
}
public static void main(String[] args) {
Main obj = new Main();
obj.display1(); // No parameters
obj.display2(24); // Single parameter
}
}
Method without parameter
Method with a single parameter: 24
Note: Java requires that argument types match the parameter types.
Java includes built-in methods available in the Java Class Library (JCL), which comes with the JVM and JRE.
public class Main {
public static void main(String[] args) {
System.out.println("Square root of 4 is: " + Math.sqrt(4));
}
}
Square root of 4 is: 2.0
Define once, use multiple times.
private static int getSquare(int x) {
return x * x;
}
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Square of " + i + " is: " + getSquare(i));
}
}
Square of 1 is: 1
Square of 2 is: 4
Square of 3 is: 9
Square of 4 is: 16
Square of 5 is: 25
Grouping code into methods makes it easier to read and debug.