java - java中通过函数调用给data赋值之前不需要使用new显式分配内存吗

标签 java memory-management initialization new-operator

一共有三个类。

public class A
{
    private B b;
    private C c;

     public void abcMethod(){
        c = new C(this);
        b = c.xyzMethod(); // Doubt is here.
    }
}

public class B
{
    int i;
    public void setI( int i){
        this.i=i;
    }
    public int getI(){
        return i;   
    }         
}

public class C
{
     public B xyzMethod(){
        B b = new B();
        b.setI(10);
        return b;
    }
}

您可能已经注意到我对代码的疑问了。我的问题是—— 1)为什么不需要“new”为对象b分配内存?

可能的答案:“是否是因为作为数据成员,在创建 A 对象时已经为其分配了内存。”

2) 我的可能答案是否正确?如果它是正确的,为什么我们使用 new 为对象 c 分配内存?也许,这是由于“this”要传递给对象的原因。因此,为了正确初始化,我们使用 new 和参数“this”。

3) 如果我在上面的第 2 点是正确的,那么先前分配给对象 c 的内存会发生什么,因为 new 将分配新的内存块并将新地址分配给 c?垃圾收集了吗?

4) 因此,从上面的观点来看,我们可以说在创建包含类的对象时,为包含类的成员对象分配了内存。我们可以直接给它们赋值,而不需要调用 new 吗?

[我是 C++ 出身,在 C++ 中不会发生这种情况。如果一个类有指向其他类的指针或引用,我们需要显式地为它们分配内存。]

请纠正我的错误或我的理解中遗漏的地方。 谢谢并致以诚挚的问候。

最佳答案

1) why "new" is not required to allocate memory for the object b?

通过方法c.xyzMethod();创建一个新的B

public B xyzMethod(){
    B b = new B();
    b.setI(10);
    return b;
}

正如您所看到的,此方法创建了一个功能齐全的 B,为其分配了内存并传递了对它的引用。对象 A 完全有权保留对此对象的引用。这和在一行上声明变量和初始化对象没有区别

2) Is my probable answer is correct? If its correct, why have we allocated memory to object c using new? Perhaps, its due the reason that "this" was to be passed to the object. So for proper initialization we used new with argument "this".

重要的是要记住,对象 A 不需要为对象 B 分配内存,类 A 所需要的只是对对象 B 的引用内存。对象 B 可以保存在其他地方,并且许多A 可以共享相同的 B(尽管您的代码中并非如此)

3) If I am correct at point 2 above, what does happen to the previously allocated memory to object c as new will allocate new chunk of memory and assign the new address to c? Garbage collected?

假设其他地方没有对该对象的引用(您的代码中就是这种情况),它确实有资格被垃圾收集

4) So from above points, we can say memory is allocated for the objects which are members of a containing class, at the time of object creation of the containing class. And we can directly assign value to them we out calling new on them?

尽管实际上您可以认为对象“包含”其他对象,但这只是人类看待它的方式。例如,以下循环依赖关系完全没问题(尽管有时不明智):

public class A {
    private B b;

    public A() {
    }

    public B getB() {
        return b;
    }

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

    public void main(String[] args){
        A a=new A();
        B b=new B();

        a.setB(b);
        b.setA(a);

        //because all that a and b contain is references this circular referencing
        //doesn't blow up

        //this line is stupid, but perfectly valid
        A referenceToAnA=b.getA().getB().getA().getB().getA();

    }

}

public class B {
    private A a;

    public B() {
    }

    public void setA(A a) {
        this.a = a;
    }

    public A getA() {
        return a;
    }


}

最后的注释

就像在 C++ 中一样,您必须使用 new 关键字为新对象分配内存,但是,没有必要释放该内存,垃圾收集会处理该问题

关于java - java中通过函数调用给data赋值之前不需要使用new显式分配内存吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20987951/

相关文章:

java - Camel 路线和终点

java - 将 SQL 数据库中的表值插入到 ArrayList

ios : change window. rootViewController 和内存管理

java - 在 RESTEasy Web 服务器上缓存数据?

java - 是否可以使用 Firebase Admin SDK 将消息发送到 FCM 客户端的一批 token ?

r - 节省内存的 scale() 函数

iphone - 在 App Delegate 中,我需要释放我的 "window"和 "navigationController"吗?

c# - 如何在 MVC 3 中测试区域注册逻辑?

c++ - 基类成员初始化

c# - 如何初始化一个使用命名参数的 `HashTable` 对象?