Java Methods
Java methods are named blocks of code that perform a task. You write a method once and call it as many times as you need, which keeps programs short, organised and easy to test.
Defining and Calling a Method
For now our methods are static so they can be called directly from main. The parts are: modifiers, return type, name, and parameters in parentheses.
public class Main {
static void greet(String name) {
System.out.println("Welcome, " + name + "!");
}
public static void main(String[] args) {
greet("Bilal");
greet("Hina");
}
}Welcome, Bilal! Welcome, Hina!
Return Values
A method with a return type other than void must return a value of that type. return also ends the method immediately.
public class Main {
static double area(double width, double height) {
return width * height;
}
static boolean isAdult(int age) {
return age >= 18;
}
public static void main(String[] args) {
double room = area(4.0, 3.5);
System.out.println("Room area: " + room);
System.out.println(isAdult(16));
}
}Room area: 14.0 false
Pass by Value
Java always passes a copy of the argument. For primitives, the method cannot change the caller's variable. For objects, the copy is of the reference, so the method can modify the object's contents but cannot replace the caller's object.
import java.util.Arrays;
public class Main {
static void tryChange(int n, int[] arr) {
n = 99;
arr[0] = 99;
}
public static void main(String[] args) {
int x = 1;
int[] data = {1, 2};
tryChange(x, data);
System.out.println(x + " " + Arrays.toString(data));
}
}1 [99, 2]
Method Overloading
Several methods can share a name if their parameter lists differ. Java picks the right one from the arguments.
public class Main {
static int add(int a, int b) { return a + b; }
static double add(double a, double b) { return a + b; }
static int add(int... values) {
int sum = 0;
for (int v : values) sum += v;
return sum;
}
public static void main(String[] args) {
System.out.println(add(2, 3));
System.out.println(add(1.5, 2.25));
System.out.println(add(1, 2, 3, 4));
}
}5 3.75 10
int... values is a varargs parameter: it accepts any number of int arguments and treats them as an array.
Recursion
A recursive method calls itself. It needs a base case that stops the recursion, otherwise you get a StackOverflowError.
public class Main {
static long factorial(int n) {
if (n <= 1) return 1; // base case
return n * factorial(n - 1); // recursive case
}
public static void main(String[] args) {
System.out.println(factorial(5));
System.out.println(factorial(15));
}
}120 1307674368000
Frequently asked questions
What is the difference between a parameter and an argument in Java?
A parameter is the variable in the method definition, like int age. An argument is the actual value passed when calling, like 25.
Is Java pass by value or pass by reference?
Java is always pass by value. With objects, the value passed is a copy of the reference, so the method can change the object but not which object the caller holds.
Can a Java method return multiple values?
Not directly. Return an array, a collection, or, best of all, a small record that groups the values with clear names.