You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java对象内存分配理解存疑,请求答疑(附代码示例)

Clarifying Java Object Memory Allocation with Your Code Example

Hey there! Let's work through your Java object memory allocation confusion step by step, using your code as a starting point. First, let's fix up the syntax errors in your sample code to make it compile properly—this will help us talk about memory behavior clearly:

class A {
    int a;
    B b;

    public B getB() {
        return this.b;
    }

    public void setB(B b) {
        this.b = b;
    }
}

class B {
    Float amount;
    String type;

    B(Float amount, String type) {
        this.amount = amount;
        this.type = type;
    }

    // Fixed return type here—should be String, not B
    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

    // Assuming you meant an override of toString() for debugging
    @Override
    public String toString() {
        return "B{amount=" + amount + ", type='" + type + "'}";
    }
}

Now let's break down how memory is allocated when you use these classes—this is likely where your original understanding was off:

  • Stack vs. Heap Basics:

    • Primitive types (like int a in class A) and object references (like B b in class A) live on the stack when they're local variables. But when they're member variables of a class, they're stored as part of the object's data on the heap.
    • Actual objects (instances of A and B) are always allocated on the heap—never the stack.
  • When you create a B instance:

    B bInstance = new B(10.5f, "test");
    
    • The new B(...) call carves out space on the heap for the B object: this includes slots for the Float amount reference and String type reference.
    • The bInstance variable (a reference that points to the heap object) lives on the stack if it's a local variable, or as part of another object on the heap if it's a member variable.
    • The Float wrapping 10.5f and the String "test" are separate heap objects too—your B instance just holds references to them, not the actual values.
  • When you link an A instance to a B instance:

    A aInstance = new A();
    aInstance.setB(bInstance);
    
    • new A() allocates heap space for the A object: this includes the primitive int a (which defaults to 0) and the B b reference (which defaults to null until you call setB).
    • When you run setB(bInstance), you're copying the reference value of bInstance into the b member of aInstance. Now both bInstance (stack) and aInstance.b (heap, part of the A object) point to the exact same B object on the heap.
  • Key Reference Behavior to Note:
    If you later do this:

    B anotherB = new B(20.0f, "newTest");
    aInstance.setB(anotherB);
    

    You're not modifying the original B object that bInstance points to—you're just updating the reference stored in aInstance.b to point to the new B object. The original B object will be eligible for garbage collection if no other references point to it.

  • Common Misconception Bust:
    Java is always pass-by-value. When you pass an object to a method (like setB(B b)), you're passing the value of the reference, not the object itself. So modifying the parameter b inside the method (e.g., b = new B(...)) won't change the original reference outside—but modifying the object's members (e.g., b.setType("updated")) will affect the actual heap object, since both references point to the same thing.

If you had a specific unexpected behavior in your code that threw you off, feel free to share those details, and we can dig even deeper!

内容的提问来源于stack exchange,提问作者Ashish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:06:16