Understand Java's pass-by-value mechanism, how objects and primitives are passed to methods, and how to work effectively with method parameters.
Java is strictly pass-by-value, which means that method parameters always receive a copy of the value being passed. Understanding this mechanism is crucial for writing effective Java code.
When passing primitive types (int, boolean, double, etc.), a copy of the actual value is passed to the method. Changes to the parameter inside the method do not affect the original variable.
public void modifyPrimitive(int x) {
x = x + 10; // Only modifies the local copy
System.out.println("Inside method: x = " + x);
}
public static void main(String[] args) {
int num = 5;
System.out.println("Before method call: num = " + num);
modifyPrimitive(num);
System.out.println("After method call: num = " + num); // Still 5
}
When passing reference types (objects), Java copies the reference value (memory address), not the object itself. This means:
class Person {
String name;
Person(String name) {
this.name = name;
}
}
public void modifyObject(Person p) {
p.name = "Modified"; // Modifies the actual object's state
}
public static void main(String[] args) {
Person person = new Person("Original");
System.out.println("Before: " + person.name);
modifyObject(person);
System.out.println("After: " + person.name); // "Modified"
}
public void reassignReference(Person p) {
p = new Person("New Person"); // Only reassigns the local copy
System.out.println("Inside method: " + p.name);
}
public static void main(String[] args) {
Person person = new Person("Original");
System.out.println("Before: " + person.name);
reassignReference(person);
System.out.println("After: " + person.name); // Still "Original"
}
A common misconception is that Java passes objects by reference. In true pass-by-reference, you would be able to reassign the original variable, which is not possible in Java.
// Primitive parameters
int x = 10;
increment(x); // x is still 10
// Reference parameters
Person p = new Person("John");
changeName(p); // p.name is now "Jane"
replacePerson(p); // p still refers to the original Person object
void increment(int num) {
num = num + 1; // Only affects local copy
}
void changeName(Person person) {
person.name = "Jane"; // Modifies the actual object
}
void replacePerson(Person person) {
person = new Person("New Person"); // Only reassigns local reference
}
Some Java objects like String, Integer, etc., are immutable. Their state cannot be modified after creation. Methods that appear to modify them actually create new objects.
public void manipulateString(String s) {
s = s + " World"; // Creates a new String object
System.out.println("Inside method: " + s);
}
public static void main(String[] args) {
String greeting = "Hello";
manipulateString(greeting);
System.out.println("After method: " + greeting); // Still "Hello"
}
Arrays are objects in Java. You can modify their contents but not reassign the original reference.
public void modifyArray(int[] arr) {
arr[0] = 100; // Modifies the actual array
arr = new int[]{200, 300}; // Only reassigns local reference
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
modifyArray(numbers);
System.out.println(numbers[0]); // 100
System.out.println(numbers.length); // 3, not 2
}