← Back to Home

Module 4: Pass-by-value

Module Overview

Understand Java's pass-by-value mechanism, how objects and primitives are passed to methods, and how to work effectively with method parameters.

Learning Objectives

Pass-by-value Explained

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.

Primitive Types

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
}
                

Reference Types (Objects)

When passing reference types (objects), Java copies the reference value (memory address), not the object itself. This means:

  • You can modify the object's state through the copied reference
  • You cannot reassign the original reference variable

Example 1: Modifying Object State


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"
}
                

Example 2: Reassigning Reference


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"
}
                

Common Misconceptions

Java is NOT Pass-by-Reference

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.

Parameter Behavior Summary

  • Primitives: The value is copied, and changes to the parameter don't affect the original
  • Objects: The reference value (memory address) is copied. You can modify the object's state, but reassigning the parameter doesn't affect the original reference variable

Visual Explanation


// 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
}
                

Special Cases

Immutable Objects

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

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
}
                

Key Topics

Parameter Passing in Java

  • Primitive vs Object parameters
  • Reference vs Value passing
  • Method parameter behavior
  • Object state modification
  • Common pitfalls and solutions

Resources

Practice Exercises

  • Experiment with primitive parameter passing
  • Work with object parameter passing
  • Modify object state through methods
  • Debug pass-by-value related issues

Next Steps

After completing this module:

  1. Complete the practice exercises above
  2. Review the additional resources for deeper understanding
  3. Attend the code-along session for hands-on practice
  4. Prepare for the Sprint Challenge by reviewing all modules