A method is reusable code that performs a clear task. You have already used built-in methods such as System.out.println(), String.length(), and Math.max(). Now we define our own methods.
Defining methods
Use returnType methodName to define a method. A method can receive input and return output.
public class Main {
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.
KNOWLEDGE QUIZ
Methods without return values
Some methods perform an action without returning a useful result.
public class Main {
static void printGreeting(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
printGreeting("Alice");
}
}A method declared with void does not return a result; control returns to the caller when the method finishes.
Method calls
When a method is called, the program temporarily jumps into that method. When it finishes, execution returns to the original position. This can be understood through the call stack.
Caller → method → return
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
The following program splits the distance calculation into distance() and the helper method square(). One method can call another so a larger calculation becomes a set of focused steps.
Separate each point's two coordinates with a space, for example 0 0. Successive nextDouble() calls read them in order; a newline between the numbers also works.
import java.util.Scanner;
public class Main {
static double square(double value) {
return value * value;
}
static double distance(double x1, double y1, double x2, double y2) {
return Math.sqrt(square(x2 - x1) + square(y2 - y1));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first point, for example 0 0: ");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
System.out.print("Enter the second point, for example 3 4: ");
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
System.out.println("Distance: " + distance(x1, y1, x2, y2));
}
}One possible run:
Enter the first point, for example 0 0: 0 0
Enter the second point, for example 3 4: 3 4
Distance: 5.0Nested calls: distance() asks square() twice
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
main()
Every Java application starts in main(). Other static methods can be declared before or after main() inside the same class.
public static void main(String[] args) {
System.out.println("Program starts");
}Write a method that returns an absolute difference
Write absoluteDifference(num1, num2) and return the absolute difference.
- Define the method.
- Use if/else to return the non-negative difference.
- Print at least two calls.
Write a prime-checking method
Wrap prime checking in reusable isPrime(n).
- Return false for n <= 1.
- Check factors through sqrt(n).
- Read and report from main().
Calculate the day number within a year
Split the work into isLeap, daysInMonth, and dayOfYear.
- Implement leap-year logic.
- Return each month length.
- Call helpers from dayOfYear and main.