References and Method Parameters
References
In Java, references are variables that point to objects stored in memory. Think of a reference as an address that tells the program where to find the actual object data.
- Primitive Types:
int,double,char, etc., store actual values. - Reference Types: Objects like
String, Arrays, custom classes, etc., store references to where the data is located in memory.
int number = 10; // 'number' holds the value 10
String text = "Hello"; // 'text' holds a reference to the String object "Hello"
Pass by Value
Java uses pass by value for all method arguments. This means that when you pass a variable to a method, Java creates a copy of that variable's value.
- For Primitives: The actual value is copied.
- For Objects: The reference (address) is copied, not the actual object.
Key Point: Java does not support pass by reference. Even when passing object references, it's the reference that's copied by value.
Example with Primitive Data Type
public class PassByValueExample {
public static void main(String[] args) {
int original = 5;
modifyValue(original);
System.out.println("After modifyValue: " + original); // Outputs: 5
}
public static void modifyValue(int number) {
number = 10;
}
}
- original is passed to modifyValue.
- Inside the method, number is a copy of original.
- Changing number to 10 does not affect original.
- Output remains 5.
Pass by Reference
While Java does not support pass by reference, it allows passing object references by value. This means the method receives a copy of the reference, pointing to the same object.
- Effect: Methods can modify the object's data, but cannot change the reference to point to a new object.
Key Point: Only the object’s internal state can be altered, not the reference itself.
4.1 Example with an Array
public class PassByReferenceArrayExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
modifyArray(numbers);
System.out.println("After modifyArray: " + numbers[0]); // Outputs: 10
}
public static void modifyArray(int[] arr) {
arr[0] = 10;
}
}
Explanation:
- numbers is an array (object) passed to modifyArray.
- The reference to numbers is copied, but both the original and the copy point to the same array.
- Modifying arr[0] affects numbers[0] because they reference the same object.
- Output changes from 1 to 10.
Note: This example uses an array (an object) instead of a primitive. Java cannot pass primitives by reference, but it can modify objects through their references.
4.2 Example with an Object
public class PassByReferenceObjectExample {
public static void main(String[] args) {
Person person = new Person("Alice");
modifyPerson(person);
System.out.println("After modifyPerson: " + person.getName()); // Outputs: Bob
}
public static void modifyPerson(Person p) {
p.setName("Bob");
}
}
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Explanation:
- A Person object with the name "Alice" is created and passed to modifyPerson.
- The reference to person is copied, but both the original and the copy point to the same Person object.
- Inside modifyPerson, the name of the Person object is changed to "Bob".
- Output changes from "Alice" to "Bob".
Note: Similar to the array example, the Person object is modified through its reference, demonstrating how objects can be altered when their references are passed by value.
Summary
- Java References: Variables that point to objects in memory.
- Pass by Value: Java always copies the value when passing arguments.
- Pass by Reference: Not supported in Java. Even object references are passed by value.
Semantic portal