Java Method Parameters

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 20 / 50 · English

Java Method Parameters

Separate the placeholder from the supplied value.

Step-by-step explanationsRunnable JavaPractice + answer

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.
01 / The concept

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.

02 / Step by step

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 / கோடின் விளக்கம்

CodeMeaning
greetMethod name
StringParameter type
nameParameter — 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.

Java reference fragmentJAVA
// 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));  // 15
03 / Read and predict

A 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.

Main.java · complete runnable exampleJAVA
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.

04 / Learn by changing

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.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

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.

05 / Your turn

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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.