5.1 Methods, Parameters, and Returns
A function is reusable code that performs a clear task. In Java, functions are written as methods inside a class. You have already used library methods such as System.out.println(), input.nextInt(), and Math.sqrt(). Now we define our own methods.
Defining methods
A Java method states a return type, a name, a parameter list, and a body. The num1 and num2 in parentheses are parameters; the concrete values such as 4 and 12 passed at the call are arguments.
public class MethodDemo {
static int larger(int num1, int num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
public static void main(String[] args) {
System.out.println(larger(4, 12));
System.out.println(larger(54, 33));
}
}Output:
12
54return gives a result back to the caller and ends the current method call.
Methods without return values
Some methods perform an action without returning a useful result. Their return type is written as void.
static void printBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(" |");
}
System.out.println();
if (i < 2) {
System.out.println("---+---+---");
}
}
}Output:
| |
---+---+---
| |
---+---+---
| |A void method has no meaningful return value. You can end it early with a bare return; or simply run to the end.
Method calls
When a method is called, the program temporarily leaves the current position and enters the method body. When the method finishes, execution returns to the original position. This "enter and return" process can be understood through the call stack.
The next program splits the "distance between two points" calculation into methods.
public class DistanceDemo {
static double square(double x) {
return x * x;
}
static double distance(double x1, double y1, double x2, double y2) {
return Math.sqrt(square(x1 - x2) + square(y1 - y2));
}
public static void main(String[] args) {
System.out.printf("Distance: %.1f%n", distance(0, 0, 3, 4));
}
}Output:
Distance: 5.0Math.sqrt() is a library method in Java's Math class.
Parameters and pass by value
Parameters make methods flexible: the same method receives different arguments and produces different results. Java passes arguments by value. For primitive values such as int and double, the argument's value is copied into the parameter, and changing the parameter inside the method does not affect the caller's variable.
static int squareValue(int n) {
n = n * n; // changes only the copy
return n;
}
public static void main(String[] args) {
int x = 5;
int y = squareValue(x); // x is still 5, y is 25
System.out.println(x + " " + y);
}A method can take many parameters, but return can hand back only one value. Splitting work into small methods that return values keeps main short and makes each step easy to test on its own.
The main method
A Java program usually starts at public static void main(String[] args), the entry point. The JVM calls this method when you run the class.
public static void main(String[] args) {
System.out.println("Program starts");
}