Java Method Parameters

In the last lesson you learned an introduction to methods. Those methods always did the same thing, because they had no inputs. But a real method usually needs data to work on, and that data comes in through parameters.

🤔 Why do methods need inputs?

A greet method that always prints “Hello, Alex!” only works for one person. The fix is one greet method that takes a name.

  • Without inputs, you copy the same code for every name. That is wasteful.
  • With a parameter, one method greets whoever you give it: “Riya”, “Arjun”, anyone.
  • Think of a parameter like a blank in a form: “Hello, ______ !” You fill the blank when you call it.
  • One method, many uses.

🧩 Parameters vs arguments

These two words sound alike, so people mix them up. Knowing the difference makes the docs easier.

  • A parameter is the variable in the method definition: the named slot.
  • An argument is the value you pass when you call the method: the thing that fills the slot.
  • In greet(String name), name is the parameter. In greet("Riya"), "Riya" is the argument.
  • Java copies the argument into the parameter, then the method runs.
  • Memory trick: parameter is the plan; argument is the actual value.

💡 A method with one parameter

The parameter goes inside the round brackets with a type and a name. One method now handles three people.

public class Greeter {
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Riya");
greet("Arjun");
greet("Alex");
}
}

Here is what happens, line by line:

  • static void greet(String name) declares the method; String name is the parameter, a slot of type String.
  • greet("Riya") copies "Riya" into name, so it prints “Hello, Riya!”, then ends.
  • greet("Arjun") puts “Arjun” in name, so it prints “Hello, Arjun!”.
  • Same for “Alex”. The parameter is what lets one method bend to each value.

Output

Hello, Riya!
Hello, Arjun!
Hello, Alex!

➕ Multiple parameters and why order matters

A method can take several parameters, separated by commas, each with its own type and name. Here is one that adds two numbers.

public class Calculator {
static void add(int a, int b) {
int sum = a + b;
System.out.println(a + " + " + b + " = " + sum);
}
public static void main(String[] args) {
add(5, 3);
add(10, 20);
}
}

Arguments fill parameters by position, left to right:

  • add(5, 3) puts 5 in a and 3 in b, so the sum is 8.
  • add(10, 20) puts 10 in a and 20 in b, so the sum is 30.
  • Java matches by position, not by name, so the first value always lands in the first slot.

Output

5 + 3 = 8
10 + 20 = 30

Order does not hurt add, since 5 + 3 and 3 + 5 both give 8. But it matters elsewhere:

  • divide(10, 2) gives 5, but divide(2, 10) gives 0 in integer division.
  • Same method, very different result, just because the values landed in the wrong slots.

Order and type must match

The arguments you pass must match the parameters in order and type. So add(5, 3) is fine. But add("five", 3) will not compile, because the first parameter expects an int, not a String. Pass the right type in the right position, every time.

🔒 Parameter scope: where a parameter lives

A parameter only exists inside the method where it is declared. That region is its scope.

  • It is born when the method is called and the argument is copied in.
  • It can be used on any line inside the method body.
  • It dies when the method finishes; the name is then gone.
  • Code outside cannot see it, so main cannot read a or b from add.

In this example, name exists only inside greet.

public class ScopeDemo {
static void greet(String name) {
System.out.println("Inside greet, name = " + name);
}
public static void main(String[] args) {
greet("Riya");
// System.out.println(name); // ❌ won't compile: name is not visible here
}
}

The commented line would break the build, because name does not exist in main.

  • Think of each method as a private room; the parameter is a note pinned to its wall.
  • Leave the room and the note is gone.
  • This is why two methods can both use a parameter called name without clashing.

Output

Inside greet, name = Riya

📦 How Java passes arguments: pass by value

Java passes arguments by value, which surprises most beginners.

  • The method gets a copy of the value, not the original variable.
  • So changing the parameter changes the copy.
  • The caller’s variable stays untouched.

Here we pass a plain int and try to change it.

public class PassByValue {
static void changeIt(int number) {
number = 999; // ✅ changes only the local copy
System.out.println("Inside method: " + number);
}
public static void main(String[] args) {
int x = 5;
changeIt(x);
System.out.println("Back in main: " + x);
}
}

Tracing it step by step:

  • x holds 5 in main.
  • changeIt(x) copies 5 into number, its own separate 5.
  • number = 999 changes only that copy, so it prints 999.
  • The method ends and the copy is thrown away.
  • Back in main, x is still 5, because the original was never touched.

It is like writing on a photocopy: the original page on your desk never changes.

Output

Inside method: 999
Back in main: 5

🔗 Objects and arrays: a copy of the reference

When a method changes an array, the pass-by-value rule did not break. You just need to know what the value is.

  • For a basic type like int, the value is the number, so Java copies the number.
  • For an object or array, the variable holds a reference: an address pointing to where the object lives.
  • Passing an array copies the address, so caller and parameter point at the same object.
  • The method can change the contents of that shared object, and the caller sees it.
  • The method cannot repoint the caller’s variable; setting the parameter to a new array changes only the local copy.

Both effects show up in one example.

public class PassReference {
static void editArray(int[] data) {
data[0] = 999; // ✅ edits the real array; caller sees this
data = new int[]{ 1, 2 }; // ✅ repoints only the local copy; caller does NOT see this
System.out.println("Inside method: " + data[0]);
}
public static void main(String[] args) {
int[] numbers = { 5, 10, 15 };
editArray(numbers);
System.out.println("Back in main: " + numbers[0]);
System.out.println("Length in main: " + numbers.length);
}
}

Each line inside the method teaches a different thing:

  • numbers points at an array holding 5, 10, 15.
  • editArray(numbers) copies the address into data, so both point at the same array.
  • data[0] = 999 follows that address and edits the real array to 999, 10, 15; the caller sees this.
  • data = new int[]{ 1, 2 } repoints only the local copy; numbers still points at the original array.
  • Inside, data[0] prints 1, since data now points at the new array.
  • Back in main, numbers[0] is 999 (shared edit) but numbers.length is still 3 (never repointed).

Output

Inside method: 1
Back in main: 999
Length in main: 3

The 999 proves the method edited the shared object. The length staying 3 proves it could not repoint the caller’s variable. Both come from the same rule: Java copied the value, and for objects the value is the address.

The simple summary

Java always copies. For a primitive, it copies the number, so the method cannot touch the caller’s variable at all. For an object or array, it copies the address, so the method can change the object’s contents but cannot make the caller point somewhere else.

⚠️ Common Mistakes

A few parameter slip-ups show up again and again.

Expecting a primitive to change. A method that “updates” an int changes only its local copy.

// ❌ Wrong: hoping doubleIt changes the caller's number
static void doubleIt(int n) {
n = n * 2; // only changes the local copy
}
// caller: int score = 10; doubleIt(score); // score is still 10!
// ✅ Right: return the new value and use it
static int doubleIt(int n) {
return n * 2;
}
// caller: int score = 10; score = doubleIt(score); // score is now 20

Passing arguments in the wrong order. Java matches by position, so a swap silently gives a wrong answer.

// ❌ Wrong: bottom and top are swapped, gives 0
static int divide(int top, int bottom) { return top / bottom; }
// caller: divide(2, 10); // means 2 / 10, which is 0
// ✅ Right: pass them in the order the method expects
// caller: divide(10, 2); // means 10 / 2, which is 5

Type mismatch. A wrong type does not compile; you cannot slip a String into an int slot.

// ❌ Wrong: first parameter is an int, but we pass a String
add("five", 3); // compile error
// ✅ Right: pass the type each parameter expects
add(5, 3);

Wrong number of arguments. Two parameters need exactly two arguments; one or three is a compile error.

✅ Best Practices

  • Name parameters clearly. greet(String name) beats greet(String s); the name tells the reader what to pass.
  • Keep the list short. Many parameters are easy to get wrong; group related values into one object and pass that.
  • Match order and type on purpose. Read the parameter list first so each argument lines up with its slot.
  • Do not rely on changing a primitive parameter. Pass by value never reaches the caller; use a return value (next lesson).
  • Be careful editing a passed object. The change is shared, so the caller sees it. Edit it only when you mean to.

🧩 What You’ve Learned

Nicely done. A quick recap:

  • Parameters are the named slots in the method definition; arguments are the values you pass when calling.
  • ✅ Parameters let one method do different work depending on the input.
  • ✅ A method can take multiple parameters, separated by commas; arguments fill them by position, so order and type must match.
  • ✅ A parameter has scope: it lives only inside its own method and is gone once the method ends.
  • ✅ Java passes arguments by value, so a method gets a copy of the value.
  • ✅ For a primitive, that copy is the number, so changing it inside does not change the caller’s variable.
  • ✅ For an object or array, the copy is the reference, so the method can change the object’s contents but cannot repoint the caller’s variable.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What is the difference between a parameter and an argument?

    Why: The parameter is declared in the method; the argument is the actual value supplied at the call.

  2. 2

    In add(int a, int b) called as add(10, 20), what is the value of a?

    Why: Arguments fill parameters by position, so the first argument 10 goes into a.

  3. 3

    You pass an int to a method, and the method sets it to 999. What does the caller's variable hold afterward?

    Why: Java passes a primitive by value, so the method changes only its own copy and the caller's variable stays the same.

  4. 4

    You pass an array to a method, and the method sets data[0] = 999. What does the caller see?

    Why: For an object or array, Java copies the reference, so both point to the same object and edits to its contents are shared.

🚀 What’s Next?

Now your methods can take input. But often you want a method to give back a result, like the sum of two numbers, so you can use it elsewhere. That is what return values are for. Let’s learn them next.

Java Return Values

Share & Connect