Parameters, Arguments & Return Values
Parameters, Arguments & Return Values
In the previous lesson you saw how to define and call a basic method. Now it is time to make methods genuinely useful: you will learn how to send data into a method through parameters, how to get a result out of a method through a return value, and why Java's pass-by-value rule matters every time you pass a variable to a method.
Parameters vs. Arguments
These two words are often used interchangeably in conversation, but they refer to different things:
- Parameter — the variable declared in the method signature (the placeholder).
- Argument — the actual value you pass when you call the method.
int a, int b, you must pass exactly two int-compatible values in that order.
Return Values
A method can hand a result back to its caller using the return keyword. The method's declared return type tells the compiler exactly what kind of value to expect. If the method produces no result, its return type is void.
Java Is Pass-by-Value — Always
This is one of the most important rules in Java: every argument is passed as a copy of its value. The method receives its own independent copy; changes to the parameter inside the method do not affect the original variable in the caller.
The variable x stays 10 because tryToDouble received a copy of 10, not a reference to x itself.
What About Objects?
When you pass an object, Java still passes by value — but the value being copied is the reference (the memory address of the object). This means the method can modify the object's internal state through that reference, but it cannot make the caller's variable point to a different object.
list = new ArrayList<>()) has no effect on the caller's variable. Only mutations through the reference are visible outside.
Variable Scope Inside Methods
Scope defines where a variable is accessible. In Java, every variable exists only within the block — the pair of curly braces { } — where it was declared.
The parameter x behaves like a local variable: it is created when the method is called and destroyed when the method returns. Two different methods can both have a local variable named x without any conflict — they live in completely separate scopes.
i inside one method will collide with an i inside another.
Putting It All Together
Here is a small, realistic example that combines parameters, a return value, and illustrates scope clearly:
The method is reusable, testable in isolation, and its internal variable fahrenheit is invisible to main — exactly how well-designed methods should work.
Summary
- Parameters are the placeholders in the method signature; arguments are the values you pass when calling.
- The
returnkeyword sends a value back to the caller; the return type in the signature declares what type it will be. - Java is always pass-by-value: primitives are fully copied; for objects, the reference is copied (so internal mutations are visible, but reassignment is not).
- Variables declared inside a method are local — they exist only within that method's scope and are invisible everywhere else.