Methods, Arrays & Strings

Parameters, Arguments & Return Values

15 min Lesson 2 of 14

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.
// "radius" is the PARAMETER — a placeholder declared in the signature static double circleArea(double radius) { return Math.PI * radius * radius; } public static void main(String[] args) { // 5.0 is the ARGUMENT — the real value passed at call time double area = circleArea(5.0); System.out.println(area); // 78.53981633974483 }
One parameter, one argument. Java matches arguments to parameters strictly by position and type. If the method declares 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.

// Returns an int — the sum of the two parameters static int add(int a, int b) { return a + b; // execution ends here; result travels back to caller } // Returns nothing — only produces a side effect (printing) static void greet(String name) { System.out.println("Hello, " + name + "!"); // no return statement needed for void } public static void main(String[] args) { int result = add(3, 7); // result == 10 greet("Layla"); // prints: Hello, Layla! }
Prefer returning values over printing inside methods. A method that returns a result can be reused anywhere — stored in a variable, passed to another method, or used in an expression. A method that only prints can only ever print; it cannot feed its output into other logic.

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.

static void tryToDouble(int number) { number = number * 2; // only the LOCAL copy changes System.out.println("Inside method: " + number); // 20 } public static void main(String[] args) { int x = 10; tryToDouble(x); System.out.println("After call: " + x); // still 10 — x was NOT changed }

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.

import java.util.ArrayList; static void addItem(ArrayList<String> list, String item) { list.add(item); // modifies the SAME object in memory list = new ArrayList<>(); // re-pointing the local copy — caller unaffected } public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<>(); fruits.add("apple"); addItem(fruits, "banana"); System.out.println(fruits); // [apple, banana] — add() worked // but the re-assignment inside addItem did nothing to fruits here }
Common confusion: beginners sometimes think Java is "pass-by-reference" for objects. It is not. The reference itself is copied. Reassigning the parameter (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.

static int compute(int x) { int result = x * x; // "result" exists only inside this method return result; } public static void main(String[] args) { int answer = compute(6); System.out.println(answer); // 36 // System.out.println(result); // COMPILE ERROR — "result" is out of scope here }

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.

Why scope matters: scope prevents unintended interactions between methods. Each method is a self-contained unit. You do not need to worry that a variable you name 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:

static double celsiusToFahrenheit(double celsius) { double fahrenheit = (celsius * 9.0 / 5.0) + 32.0; // local to this method return fahrenheit; } public static void main(String[] args) { double bodyTemp = celsiusToFahrenheit(37.0); System.out.printf("37 C = %.1f F%n", bodyTemp); // 37 C = 98.6 F double boiling = celsiusToFahrenheit(100.0); System.out.printf("100 C = %.1f F%n", boiling); // 100 C = 212.0 F }

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 return keyword 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.