Fundamentals 18 min read

Understanding Value Passing and Reference Passing in Java: JVM Memory Model and Parameter Behavior

This article explains the distinction between value passing and reference passing in Java by examining the JVM memory layout, the roles of the stack, heap, and method area, and demonstrates how primitive and object parameters behave through detailed code examples and visual diagrams.

Java Captain
Java Captain
Java Captain
Understanding Value Passing and Reference Passing in Java: JVM Memory Model and Parameter Behavior

The article begins by clarifying the concepts of formal parameters (形参) and actual parameters (实参) in Java, emphasizing that many developers are confused about whether Java uses value passing, reference passing, or both.

It then reviews Java data types, distinguishing primitive types (byte, short, int, long, float, double, char, boolean) from reference types (classes, interfaces, arrays), and explains how the JVM stores these types in memory.

The JVM memory model is described, showing the runtime data areas: the virtual machine stack, heap, program counter, method area, and native method stack. Each area’s purpose and the kind of data it holds are outlined.

For primitive types, the article demonstrates that local variables are stored on the stack, and assignments create new entries in the stack rather than modifying existing literals. Example code:

public static void func(int a) {
    a = 20;
    System.out.println(a);
}

public static void main(String[] args) {
    int a = 10; // actual argument
    func(a);
}

It shows that changes to a inside func do not affect the original variable because a copy of the value is passed.

For member variables, the article explains that primitive fields are stored in the heap as part of the object, so their lifecycle matches the object’s lifecycle.

Static primitive variables reside in the method area’s runtime constant pool and are loaded with the class.

Reference types are stored in the heap, while the variable that holds the reference lives on the stack. When a method receives an object reference, the reference value (the address) is copied, not the object itself.

public static void PersonCrossTest(Person person) {
    System.out.println("Incoming name: " + person.getName());
    person.setName("Zhang Xiaolong");
    System.out.println("After change: " + person.getName());
}

public static void main(String[] args) {
    Person p = new Person();
    p.setName("Ma Huateng");
    PersonCrossTest(p);
    System.out.println("After method: " + p.getName());
}

This demonstrates that modifying the object through the copied reference affects the original object because both references point to the same heap instance.

The article then adds a variant where the parameter is reassigned to a new object inside the method, showing that the original caller’s reference remains unchanged, illustrating that the reference itself is passed by value.

public static void PersonCrossTest(Person person) {
    System.out.println("Incoming name: " + person.getName());
    person = new Person(); // reassign reference
    person.setName("Zhang Xiaolong");
    System.out.println("After change: " + person.getName());
}

Finally, the author concludes that Java always uses value passing (copy of the value), whether the value is a primitive or a reference, and that the observed effects depend on whether the copied reference still points to the original object.

JavaProgrammingFundamentalsparameter passingvalue passingjvm-memoryreference passing
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.