Method Parameters
Passing arguments to methods
Interview Relevant: Pass by value vs pass by reference question
6 min read
Method Parameters in Java
Parameters are values passed to methods when called. Understanding how Java passes parameters is crucial for writing correct code.
Parameter vs Argument
- Parameter: Variable in method declaration
- Argument: Actual value passed when calling
ā ļø Critical Concept: Java is ALWAYS pass-by-value! For objects, the reference VALUE is passed, not the object itself. You can modify object state but cannot reassign the reference.
Pass by Value Explained
- Primitives: A copy of the value is passed
- Objects: A copy of the reference is passed
Code Examples
Basic parameter usage
java
1// Basic parameters
2public void greet(String name) { // 'name' is parameter
3 System.out.println("Hello, " + name);
4}
5
6greet("Alice"); // "Alice" is argument
7
8// Multiple parameters
9public int add(int a, int b) {
10 return a + b;
11}
12
13int result = add(5, 3); // 5 and 3 are arguments
14
15// Parameters with same type
16public void printRange(int start, int end) {
17 for (int i = start; i <= end; i++) {
18 System.out.print(i + " ");
19 }
20}
21
22printRange(1, 5); // 1 2 3 4 5Pass by value behavior explained
java
1// Pass by value - Primitives
2public static void changePrimitive(int x) {
3 x = 100; // Only changes LOCAL copy!
4}
5
6int num = 5;
7changePrimitive(num);
8System.out.println(num); // Still 5! (unchanged)
9
10// Pass by value - Objects (reference is copied)
11public static void modifyObject(StringBuilder sb) {
12 sb.append(" World"); // Modifies original object!
13}
14
15StringBuilder builder = new StringBuilder("Hello");
16modifyObject(builder);
17System.out.println(builder); // "Hello World" (modified!)
18
19// Cannot reassign reference
20public static void tryReassign(StringBuilder sb) {
21 sb = new StringBuilder("New"); // Only changes LOCAL reference!
22}
23
24StringBuilder builder2 = new StringBuilder("Original");
25tryReassign(builder2);
26System.out.println(builder2); // "Original" (unchanged!)Different parameter types
java
1// Different parameter types
2public class ParameterExamples {
3
4 // Primitive parameters
5 public double calculateCircleArea(double radius) {
6 return Math.PI * radius * radius;
7 }
8
9 // Object parameter
10 public void printPerson(Person person) {
11 System.out.println(person.getName());
12 }
13
14 // Array parameter
15 public int sum(int[] numbers) {
16 int total = 0;
17 for (int n : numbers) {
18 total += n;
19 }
20 return total;
21 }
22
23 // Varargs (variable arguments)
24 public int sumAll(int... numbers) {
25 int total = 0;
26 for (int n : numbers) {
27 total += n;
28 }
29 return total;
30 }
31}
32
33// Calling varargs
34sumAll(1, 2, 3); // 6
35sumAll(1, 2, 3, 4, 5); // 15
36sumAll(); // 0Final parameters and default value workarounds
java
1// Final parameters
2public void process(final int value) {
3 // value = 10; // ERROR! Cannot reassign final parameter
4 System.out.println(value);
5}
6
7// Helps prevent accidental modification
8
9// Default parameter values (NOT supported in Java!)
10// public void greet(String name = "Guest") {} // NOT VALID!
11
12// Workaround: Method overloading
13public void greet() {
14 greet("Guest"); // Call with default
15}
16
17public void greet(String name) {
18 System.out.println("Hello, " + name);
19}
20
21// Or use Optional (Java 8+)
22public void greet(Optional<String> name) {
23 System.out.println("Hello, " + name.orElse("Guest"));
24}
25
26// Or builder pattern for many optional params
27User.builder()
28 .name("Alice")
29 .age(25) // Optional
30 .build();Use Cases
- Passing data to methods
- Configuring method behavior
- Processing multiple values
- Working with object references
- Implementing callbacks
- API design
Common Mistakes to Avoid
- Thinking Java passes objects by reference
- Expecting primitive changes to persist
- Too many parameters (use object or builder)
- Modifying parameters intended to be immutable
- Not using final for parameters that shouldnt change
- Confusion between parameter and argument