Why doesn't Java initialize Array Objects?

后端 未结 2 1942
被撕碎了的回忆
被撕碎了的回忆 2021-01-20 02:43

If one runs the following code in java:

public class Testing {

    public static void main(String[] args) {
        TestObject[] array = new TestObject[4];
         


        
相关标签:
2条回答
  • 2021-01-20 03:26

    What happens if you want to fill up your array with real objects that are subclasses of TestObject, or which are constructed with non-default constructors? In the real world, you rarely want an array with a bunch of identical objects.

    0 讨论(0)
  • 2021-01-20 03:34

    With new TestObject[4] you create an array, wich can hold 4 references to TestObject. So understand the difference between TestObject[] and TestObject:

    TestObject[] is a reference store for TestObject - objects. If you create a List<TestObject> you'll have to fill up the list with references too.

    0 讨论(0)
提交回复
热议问题