Java Methods and Overloading: Parameters, return and Pass-by-Value
A method is a machine you build once and reuse. Six programs cover parameters and arguments, returning a value, 'missing return statement', early return, why doubleIt(n) leaves n at 10, and overloading print three ways.
What you will learn
- Name every part of a method: modifiers, return type, name, parameters, body and return
- Tell parameters from arguments and return a value to the caller
- Follow the return rules: every path returns, nothing runs after return
- Explain why a method changes only its copy, and overload a method correctly
Before you start
- Java loops: for, while, do-while and for-each
Think of a coffee machine. You build it once, then press the button again and again: a shot, a double, a decaf. Each press repeats the same steps. A method is that machine: a piece of code with a name that you write once and call wherever you need it. Overloading lets one name do a few different jobs.
One method, many calls
package org.studyeasy.s02l13;
public class Ex01_Loop {
static void loop(int start, int stop) {
for (int i = start; i <= stop; i++) {
System.out.println(i);
}
}
public static void main(String[] args) {
loop(1, 3);
loop(20, 22);
}
}
1
2
3
20
21
22
A loop repeats code in one place; a method goes a step further, because you can use it from anywhere. loop is the method, and start and stop are its parameters, the slots you declare. 1, 3 and 20, 22 are the arguments, the values you pass in when you call it. main is a method too: the entry point where Java always starts. For now, static just means you can call the method straight from main. Call loop(1) with one argument and javac complains that it needs two ints.
Every part of a method
A method that returns
package org.studyeasy.s02l13;
public class Ex02_Add {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int total = add(3, 4);
System.out.println(total);
System.out.println(add(10, 5) * 2);
}
}
7
30
static int add promises a whole number back, and return a + b hands it to the caller. total keeps the 7. You can also use the call directly in an expression: add(10, 5) is 15, times 2 is 30. Store the result in String total instead and javac says an int cannot be converted to String.
The rules about return
Every path must return
package org.studyeasy.s02l13;
public class Ex03_Grade {
static String grade(int score) {
if (score >= 50) {
return "Pass";
}
return "Fail";
}
public static void main(String[] args) {
System.out.println(grade(72));
System.out.println(grade(30));
}
}
Pass
Fail
If the score is under 50, the method still has to give something back. Delete return "Fail"; and javac reports "missing return statement": there's a path where nothing comes back.
return ends the method
package org.studyeasy.s02l13;
public class Ex04_EarlyReturn {
static void greet(String name) {
if (name.isEmpty()) {
return;
}
System.out.println("Hi " + name);
}
static int square(int n) {
return n * n;
}
public static void main(String[] args) {
greet("");
greet("Priya");
System.out.println(square(5));
}
}
Hi Priya
25
In a void method, a bare return; quits early, so the empty name prints nothing. And nothing runs after return: add a println below return n * n; and javac says "unreachable statement".
The method works on a copy
package org.studyeasy.s02l13;
public class Ex05_Copy {
static void doubleIt(int x) {
x = x * 2;
}
static int twice(int x) {
return x * 2;
}
public static void main(String[] args) {
int n = 10;
doubleIt(n);
System.out.println("n = " + n);
n = twice(n);
System.out.println("n = " + n);
}
}
n = 10
n = 20
When you call doubleIt(n), a photocopy of 10 travels into x. Inside, x becomes 20, but the copy is thrown away when the method ends, and n is still 10. To get the result out, return it and store it: n = twice(n) gives 20. Write just twice(n); and the result is discarded again.
Overloading: one name, several jobs
package org.studyeasy.s02l13;
public class Ex06_Overloading {
static void print(int n) {
System.out.println("int: " + n);
}
static void print(String s) {
System.out.println("String: " + s);
}
static void print(int a, int b) {
System.out.println("sum: " + (a + b));
}
public static void main(String[] args) {
print(7);
print("Emma");
print(3, 4);
}
}
int: 7
String: Emma
sum: 7
Same name, different parameters, and Java picks the version by what you pass. What counts as different?
| Pair | Allowed? | Why |
|---|---|---|
print(int) and print(String) | Yes | parameter types differ |
print(int) and print(int, int) | Yes | number of parameters differs |
print(int n) and print(int x) | No | parameter names don't count |
int half(int) and double half(int) | No | return type alone isn't enough |
Add static double print(int n) to the class and javac says print(int) is already defined.
Recap
- A method is named, reusable logic: inputs (parameters), steps (body), an optional result (return type).
- Parameters are declared; arguments are passed.
- Every path must return; nothing runs after
return. - The method gets a copy of a primitive argument. Return the result and store it.
- Overloads differ by parameters, never by return type alone.
Next: where variables live and die, in scope, code blocks and the call stack.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
