References and Method Parameters

Domains: Java Basics

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;
    }
}
  1. original is passed to modifyValue.
  2. Inside the method, number is a copy of original.
  3. Changing number to 10 does not affect original.
  4. 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.

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:

  1. numbers is an array (object) passed to modifyArray.
  2. The reference to numbers is copied, but both the original and the copy point to the same array.
  3. Modifying arr[0] affects numbers[0] because they reference the same object.
  4. 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:

  1. A Person object with the name "Alice" is created and passed to modifyPerson.
  2. The reference to person is copied, but both the original and the copy point to the same Person object.
  3. Inside modifyPerson, the name of the Person object is changed to "Bob".
  4. 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

Similar pages

Page structure
Terms

Reference

Object

Java

Arrays

Method

Arguments

Integer

String

Pass by Value

Pass by Reference

Static

Double

Parameters

Class