Parameters & Overloading
How do arguments get into a method, and what happens if the method changes them? This lesson explains Java's pass-by-value rule (which surprises many people when objects are involved), methods with a variable number of arguments, and overloading — several methods sharing a name.
Java passes by value
void addTen(int number) { number += 10; System.out.println("Inside: " + number); } void main() { int x = 5; addTen(x); System.out.println("Outside: " + x); }
Inside: 15 Outside: 5
The method receives a copy of the value. Changing the parameter doesn't affect the caller's variable. To get a result back, return it.
Passing references
import java.util.Arrays; void doubleAll(int[] values) { for (int i = 0; i < values.length; i++) { values[i] *= 2; } } void replace(int[] values) { values = new int[]{0, 0, 0}; } void main() { int[] data = {1, 2, 3}; doubleAll(data); System.out.println(Arrays.toString(data)); replace(data); System.out.println(Arrays.toString(data)); }
[2, 4, 6] [2, 4, 6]
For arrays and objects, the value copied is the reference. The method can use it to modify the same array the caller sees (doubleAll), but reassigning the parameter to a new array only changes the method's copy of the reference (replace). Java is always pass-by-value; sometimes the value is a reference.
Varargs
int sum(int... numbers) { int total = 0; for (int n : numbers) total += n; return total; } void main() { System.out.println(sum()); System.out.println(sum(4)); System.out.println(sum(1, 2, 3, 4)); System.out.println(sum(new int[]{10, 20})); }
0 4 10 30
int... numbers accepts any number of arguments, which arrive as an array. A varargs parameter must be the last one. String.format and List.of use this.
Overloading
double area(double radius) { return Math.PI * radius * radius; } double area(double width, double height) { return width * height; } String describe(int n) { return "int " + n; } String describe(double n) { return "double " + n; } String describe(String s) { return "text " + s; } void main() { System.out.println("%.2f".formatted(area(1.5))); System.out.println(area(3, 4)); System.out.println(describe(7)); System.out.println(describe(7.0)); System.out.println(describe("7")); }
7.07 12.0 int 7 double 7.0 text 7
Methods can share a name if their parameter lists differ in number or types. The compiler picks the best match for each call. The return type alone isn't enough to tell overloads apart.
Default values by overloading
String formatPrice(double amount) { return formatPrice(amount, "EUR"); } String formatPrice(double amount, String currency) { return "%.2f %s".formatted(amount, currency); } void main() { System.out.println(formatPrice(12.5)); System.out.println(formatPrice(12.5, "GBP")); }
12.50 EUR 12.50 GBP
Java has no default parameter values; a short overload that calls the full version with a default is the usual substitute.
final parameters
int twice(final int n) { n = n * 2; return n; } void main() { System.out.println(twice(4)); }
Main.java:2: error: final parameter n may not be assigned
Marking a parameter final prevents accidental reassignment. Many teams treat parameters as read-only by convention even without the keyword.
Try it yourself
Write overloaded max methods for two and three numbers, and a varargs version.
9 9 23