且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

数组对象和普通对象初始化

更新时间:2022-02-10 09:53:53


我有3个对象root1,root2,root3

I have 3 objects root1,root2,root3

不,你没有。你有三个变量,,它们都是空的。

No you don't. You have three variables, and they are all null.


它们出现在 root []

root [] $中没有任何内容c $ c>初始化时除了三个空值。

Nothing is present in root[] except three nulls when you initialize it.


我初始化了节点类使用 root [0]

您已经实例化 节点类,并将引用存储到 root [0]

You have instantiated the Node class, and stored the reference into root[0].

// It is printing null value.

使用此代码,它应该抛出 NullPointerException

With this code it should be throwing NullPointerException.

root[0]=root1

不,不是。你在循环中覆盖它,无论如何当你进行(无意义的)数组初始化时, root1 为null,因为你没有初始化它。

No it doesn't. You overwrote it in your loop, and in any case when you did the (pointless) array initialization, root1 was null, as you didn't initialize it.