8

Hello I would like to know how many objects are created with this array?

String arr[] = {"Paul", "Steven", "Jennifer", "Bart"};

Thanks in advance!

6 Answers 6

12

Nine objects are created.

Each String is TWO objects. The String reference, and the String's underlying char[]. So for 4 strings, that's 8 objects.

Then, there is the String[] itself for a total of 9.

This of course assumes the String literal has not been intern()ed yet by the JVM. If it has, then it will not create the String, but instead pull it from the intern pool, which could give you a total of 1, 3, 5, 7, or the original 9 objects created, depending on how many Strings are interned.

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

17 Comments

Or maybe implementation depended?
It could be more, although I don't see how it could be less. You will at minimum need the String[] reference, a reference for each String and one for each char[] that backs it. I can't think of what other objects they'd put in there (And checking source has shown there to be none) although it's conceivable they could.
The strings may have already been created in the JVM, and the array may just borrow the reference directly. So Lenik's right, it's JVM implementation dependent.
@Tim that's what i thought too. Whoever came up with this as a brain teaser deserves some credit, but whoever puts this on an exam should be shot.
@Sebastian One thing to consider is that it may have been written by a TA who has never had a job, and has zero critical thinking skills. He may not realize there's actually 9 Objects. That being said, it's sad how little critical thinking skills are really taking place. I had to fight with someone about serializing Strings. He said "Two strings of length 16 should take 64 bytes of data." That makes sense, 16*2 bytes per char*2 Strings = 64 bytes. But it turns out it's more like 400+ due to some overhead. Little things can matter, but many devs really lack critical thinking skills.
|
2
String arr[] = {"Paul", "Steven", "Jennifer", "Bart"};
for (Object o : arr) {
   System.out.format("%d\n", o.hashCode());
}
System.out.format("%d\n", arr);

You should get 5 distinct hashCode. A strong suggestion that there now exists 5 objects in your heap.

2 Comments

Thanks your answer was right from the beginning, others here were just going more into detail, but thanks for the advice with the hashcodes...
How many objects exist after the statement does not -by itself- give information about how many objects were created by the statement.
2

Between 1 and 5 depending on JVM implementation and state. A new String array is apparently created, the Strings - we don't know.

EDIT: As someone pointed out in other answers. 1-9 would be a more precise answer. Since you create a char[] inside a String when it gets constructed.

Comments

1

The answer is none, because Array can't create objects, only new can. :P

4 Comments

Opcodes are specified in the JVM spec to work by pushing an appropriately-constructed String object onto the stack. It's implementation dependent, and is possible that if the Strings have been used before that a String that's already been intern()ed won't have to be created, but we can't rely on that.
Yes probably but just for primitive data types I think, since String are Objects naturally, you wouldn't need 'new' for creating a new Object.
For clarity, I was joking, hence the ":P". I will gladly accept down votes as punishment.
Ohh okay lol I was checking other pages and they were saying 4, which would be impossible, since the array is an object as well, or others also said none, so I was wondering
1

Depending on how you look at it, you could say 9 objects or just one. If you look at this array in a debugger you will be able to see 9 objects, the array, the String objects and the char[] in those char[].

However the String literals are in a pool and are not created each time (only once) So if you run this line many times you will be only creating the array each time. i.e. only one additional object is created.

Comments

0

HI,

Five objects are created.

If you are using

int[] i = new int[5];

then the jvm will create one object on heap.

But if you are providing elements to array for e.g.

i[0] = 1;
i[1] = 2;
.
.
i[4] = 5;

then the jvm will create six object with five integer and one array object on heap.

5 Comments

Exactly but you need the 'new' inside of the creation otherwise there would be none, alright thanks guys... Messed up my Java exam :P I really thought 5 but now I remember it has to be with 'new' to actually create the array...
Ya you can have a look at this link for more help [coderanch.com/t/418315/java-programmer-SCJP/certification/…
No, in this case no objects will be created when you assign values, because int is not an object, but a pprimitive type!
@Sebastian, the code sample you posted includes some syntactic sugar that automatically creates the String Array with size 4, so {"a","b","c","d"} is a String[4].
@Joachim Sauer :Thanx, you are correct but that was just and example and I also provided a nice link for him to understand it soundly.

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.