3

If one runs the following code in java:

public class Testing {

    public static void main(String[] args) {
        TestObject[] array = new TestObject[4];
        //array[0] = new TestObject();
        System.out.println(Arrays.asList(array));
    }
}

class TestObject {
    String aString;

    public TestObject() {
        aString = "This has been initialized.";
    }
}

It will print (null, null, null, null), and if array[0] = new TestObject(); is uncommented, then the first object will have a memory address (and not be null). I'm just confused to as to why Java wouldn't automatically call the constructor for each Object in an array when the array is first initialized properly. What are the advantages of the way it works right now? Is it a space issue (as in it would be too costly to do so)?

Maybe I've just overlooked something silly or I'm simply mistaken. This is not directly related to a problem I'm having, so if it's the wrong forum I apologize.

11
  • 2
    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. Commented Jul 21, 2012 at 4:14
  • 3
    It does initialize them ... just to the default value of the type. Doing otherwise quickly breaks down when sub-types or non-default constructors are used. Commented Jul 21, 2012 at 4:15
  • 1
    @AmirAfghani If you want 10 empty strings .. since the size of a List can [usually] change, one generally just doesn't add "empty values" to it .. (as that can be represented by lack-of-item-in-list in most cases). In any case there are various Array.fill methods. Commented Jul 21, 2012 at 4:17
  • 1
    I think @AmirAfghani's point is that filling any collection class with a bunch of identical objects is almost never what you want. Why create objects that you're immediately going to have to replace (and GC)? Commented Jul 21, 2012 at 4:21
  • 1
    Or the constructor could be private, or there might not be a default constructor, or... Commented Jul 21, 2012 at 4:25

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Additionally, not all objects have "default constructors."
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.