Hello I would like to know how many objects are created with this array?
String arr[] = {"Paul", "Steven", "Jennifer", "Bart"};
Thanks in advance!
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.
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.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.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.
The answer is none, because Array can't create objects, only new can. :P
String that's already been intern()ed won't have to be created, but we can't rely on that.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.
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.
int is not an object, but a pprimitive type!