Menu

Java Method Parameters: Arguments, Pass-by-Value, and Return

How Java method parameters work - passing arguments, the difference between parameters and arguments, Java's pass-by-value rule, and returning values.

This page includes runnable editors - edit, run, and see output instantly.

Parameters Let a Method Take Input

A method that always does the same thing is limited. Parameters are the inputs you declare inside the method's parentheses so the same method can work on different values each time you call it.

You list each parameter with a type and a name. Inside the method body, a parameter behaves like an ordinary local variable that already holds whatever the caller passed.

name is the parameter. "Sam" and "Alex" are the arguments - the concrete values supplied at the call. One method definition, two different outputs.

Parameters vs Arguments

These two words get used loosely, but the distinction is worth keeping straight:

  • A parameter is the placeholder in the declaration: String name.
  • An argument is the value you actually pass: "Sam".

So void greet(String name) declares a parameter, and greet("Sam") passes an argument. When someone says "this method takes two parameters," they mean the declaration; "I passed two arguments" means the call.

Multiple Parameters

Separate parameters with commas, and give each one its own type - you cannot share a type across two parameters the way you might in a variable declaration:

Arguments are matched to parameters strictly by position, not by name. describe("Sam", 30, true) works because the order lines up with (String name, int age, boolean active). Swap two arguments of compatible types and you get a silent logic bug, not a compile error - so order matters.

A common mistake is writing int add(int a, b). Each parameter needs its own type: int add(int a, int b).

Returning a Value

Parameters are how data flows in; the return value is how a result flows back out. Declare the return type before the method name, and use return to hand a value back to the caller:

The return type (double) must match what you actually return. A method declared void returns nothing - it can still use a bare return; to exit early, but it cannot return a value. The moment return runs, the method ends and control jumps back to the call site.

Java Is Pass-by-Value

This trips up almost everyone at first. When you pass an argument, Java copies the value into the parameter. For primitives, that means the method gets its own copy - changing the parameter never touches the caller's variable:

x stays 5. The method modified its own copy of the number, not the original.

Passing Objects: Still by Value, but Watch the Reference

Objects feel like an exception, but they are not. What gets copied is the reference (the arrow pointing at the object), not the object itself. So the method points at the same object and can mutate it - which is exactly why adding to a shared ArrayList inside a method is visible to the caller:

Two different outcomes, one rule. addItem follows the reference and changes the actual list, so the caller sees new appear. replace reassigns the parameter to a brand-new list - but that only repoints the local copy of the reference, so the caller's list is untouched. The takeaway: you can change an object through a parameter, but you can never make the caller's variable point somewhere new.

Next: Method Overloading

You now know how to feed a method exactly the inputs it needs. But what if you want one method name to accept different sets of parameters - say print(int) and print(String)? That is method overloading, and it is the next page.

Frequently Asked Questions

What is the difference between a parameter and an argument in Java?

A parameter is the variable named in the method declaration - String name in void greet(String name). An argument is the actual value you pass when you call the method - greet("Sam") passes the argument "Sam". In short: parameters live in the definition, arguments live in the call.

Is Java pass-by-value or pass-by-reference?

Java is always pass-by-value. For primitives, a copy of the value is passed, so reassigning the parameter cannot change the caller's variable. For objects, a copy of the reference is passed - you can mutate the object the reference points to (e.g. add to a list), but reassigning the parameter itself does not affect the caller's variable.

How do you pass multiple arguments to a method in Java?

List the parameters in the declaration separated by commas, each with its own type: int add(int a, int b). Call it with arguments in the same order: add(2, 3). Java matches arguments to parameters strictly by position, not by name, so the order must line up.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED