Java Method Parameters
Separate the placeholder from the supplied value.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Method Parameters.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
A parameter is a named input in the method declaration. An argument is the value supplied at a call site. Java passes values by value, including a copy of an object reference; changing a parameter does not reassign the caller variable.
Explore the details
Parameters and Arguments
Parameters are the variables listed in the method definition. Arguments are the actual values you pass when calling the method.
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
greet | Method name |
String | Parameter type |
name | Parameter — acts as a local variable inside the method |
"Sathish" | Argument — the real value passed at call time |
Multiple Parameters
Return Values
Parameter = variable in the definition. Argument = value at the call site. Arguments must match parameters in number, order, and type.
// Pass by value — original unchanged
static void doubleIt(int n) { n = n * 2; }
int x = 5;
doubleIt(x);
System.out.println(x); // 5 — not changed
// Return a value
static int square(int n) { return n * n; }
System.out.println(square(7)); // 49
// Varargs — variable number of args
static int sum(int... nums) {
int total = 0;
for (int n : nums) total += n;
return total;
}
System.out.println(sum(1, 2, 3, 4, 5)); // 15A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int value = 5;
Demo.doubleIt(value);
System.out.println(value);
System.out.println(Demo.doubled(value));
}
}
class Demo {
static void doubleIt(int n) { n = n * 2; }
static int doubled(int n) { return n * 2; }
}Predict first. Then reveal the expected output / வெளியீடு
5 10
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Assign the returned result back to value, then print it.
Reveal the answer and reasoning / விடை
Use value = Demo.doubled(value);. The caller variable now contains 10.
One mistake to avoid: Reassigning a parameter changes the local parameter, not the caller variable.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.