Fundamentals 10 min read

Understanding Object References, Memory Allocation, Garbage Collection, and Parameter Passing in Java

This article explains how Java's `new` keyword allocates objects on the heap, how object references reside on the stack, the relationship between references and objects, the mechanics of reference assignment, garbage collection conditions, and why Java uses value‑passing for method parameters.

Java Captain
Java Captain
Java Captain
Understanding Object References, Memory Allocation, Garbage Collection, and Parameter Passing in Java

Object Reference

Previously we used the concept of an "object" without discussing how it is stored in memory; this leads to the important notion of an object reference .

We continue to use the Human class defined earlier and introduce a Test class:

public class Test {
    public static void main(String[] args) {
        Human aPerson = new Human(160);
    }
}

class Human {
    /**
     * constructor
     */
    public Human(int h) {
        this.height = h;
    }

    /**
     * accessor
     */
    public int getHeight() {
        return this.height;
    }

    /**
     * mutator
     */
    public void growHeight(int h) {
        this.height = this.height + h;
    }

    private int height;
}

External code can create an object of Human , as shown above:

Human aPerson = new Human(160);

This creates a Human object named aPerson . Several details deserve deeper discussion:

The right‑hand side uses new , which allocates space for the object on the heap . The heap stores the object's data and methods.

The left‑hand side, aPerson , is an object reference that lives on the stack . It is not the object itself but a pointer‑like value that refers to the heap object.

During assignment, the address of the heap‑allocated object is copied into the reference variable.

The memory referred to is the JVM (Java Virtual Machine) process memory.

Stack access is faster than heap access, but stack data is limited to the lifetime of the current call frame. In Java, all ordinary objects reside on the heap, so the new keyword always creates an object on the heap.

Primitive types (e.g., int , double ) are stored directly on the stack; their variable names represent the actual data, not a reference.

The relationship between a reference and its object is like a kite and a person: the kite (reference) is what we see, while the person (object) is underneath.

Even though references and objects are separate, every access to an object must go through its reference (e.g., reference.method() ). If a field of object a holds another object b , the field actually stores a reference to b .

In Java, a reference behaves like a pointer, but you cannot manipulate the pointer value directly as in C; you can only use the reference to operate on the object, which prevents many pointer‑related errors.

Reference Assignment

Assigning one reference to another copies the object's address, so both references point to the same object. For example, dummyPerson = aPerson; makes dummyPerson refer to the same Human instance:

An object can have multiple references. Modifying the object through one reference is visible through all others. The following Test class demonstrates this:

public class Test {
    public static void main(String[] args) {
        Human aPerson = new Human(160);
        Human dummyPerson = aPerson;
        System.out.println(dummyPerson.getHeight());
        aPerson.growHeight(20);
        System.out.println(dummyPerson.getHeight());
    }
}

Changing aPerson also changes dummyPerson because they reference the same object. Assigning a reference does not duplicate the object; a separate cloning mechanism is required for that.

Garbage Collection

When a method finishes, its stack‑based references and primitive variables are cleared. Objects live on the heap, so they remain until no references point to them. The JVM’s garbage collector reclaims memory occupied by such unreachable objects.

The basic rule is: an object is not collected while at least one reference points to it; once all references are gone, the object becomes eligible for garbage collection.

In the illustration, a Human object has three references (two from the stack and one from another object’s field). A Club object has no references and would be reclaimed, causing any references it held to become null as well.

Parameter Passing

After separating references from objects, Java’s parameter‑passing mechanism becomes clear: Java uses pass‑by‑value . When a method receives a parameter, it gets a copy of the value.

Two kinds of values are passed:

For primitive types, the actual data value is copied; modifications inside the method do not affect the original variable.

For object references, the reference (i.e., the address) is copied; the method can use this reference to modify the underlying object, and those changes are visible to the caller.

If a method creates a new object with new and returns its reference, the returned reference keeps the object alive because it is no longer unreferenced.

Summary

new creates an object on the heap and returns a reference stored on the stack.

References point to objects; objects are reclaimed only when no references exist.

Garbage collection frees memory of unreachable objects.

Java method arguments are passed by value: primitives are copied directly, while object references are copied, allowing the method to affect the original object.

Original source: cnblogs.com/vamei/archive/2013/04/01/2992484.html

Java Group

Daily Java knowledge sharing – WeChat ID: javatuanzhang.

Javamemory managementgarbage collectionparameter passingobject reference
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.