views:

261

answers:

3

I get annoying to comprehend memory allocation of array in Java,could your guys give me some clarification about this.Following is an example that i want you to give me the result of how many object get instantiated for each statement.

String s1[] = {"12","saf"};

int s2[] = {1,2};

Object s3[] = new Object[2];

String s4[][] = {{"12","2d"},{"saf"}};

String s5[][] = {{"12","2d"},{"saf","fds"}};

Object s6[] = {new Object[2],new Object[2]};
A: 

It largely depends on what you mean by "instantiated" in this context. All of those string literals will be String objects but they aren't necessarily "instantiated" in the conventional sense as they are part of the class's constant pool.

So, not including those, it looks like:

1 1 1 3 3 3

Using the last one as an example:

new Object[2]

1

new Object[2]

1 And one more for the array that contains them.

PSpeed
At this point people are often not ready to take into account calculations for the constant string pool, so this gets confusing although it's technically correct, so I'm certainly glad you posted it. Readers of this answer in context of the original question should also check out this http://stackoverflow.com/questions/1881922/questions-about-javas-string-pool and this http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html and similar things so not to get mixed up. I think author posted strings by happenstance (for convenience) but maybe it's academic.
John K
thanks for your rapid answer.
didxga
@jdk, I figured that... which was why I left them out of my object counts. :) But I thought I'd at least hint at why since technically they are objects and they are "instantiated".
PSpeed
+3  A: 
  1. One string array is instantiated. It holds two references to interned strings.

  2. One int array is instantiated. It holds two ints.

  3. One object array is instantiated. It holds two object references, which are both null.

  4. Two string arrays are instantiated, and one array of string arrays.

  5. Same as 4.

  6. Three object arrays are instantiated. One contains the other two.

Anon.
Thanks Anon.,if i replace the first statement "String s1[] = {"12","saf"};" with "String s1[] = {new String("12"),new String("saf")};"so it will instantiate three objects,right?
didxga
+2  A: 

It's important to know differences between declaring vs allocating an array, and initialization vs assigning values to it.

Declaring an array is stating its dimensions, also creating a variable that can hold a reference to one.
The array object itself is allocated (memory carved out for it and its elements, and its construction is run) when the new keyword is used because array is a reference type. If you skip new then array variable itself contains null (which differs from an empty array having 0 elements).

When array is allocated, all elements receive the default value of the array's data type - so 0 for all elements of an int[ ] and numeric types, null for object[ ] and other reference types, false for bool[ ], etc.

However if you { initialize an array using braces }, you put values directly into the elements and skip them receiving the default value. So initialization will occur explicitly if you provide the values otherwise implicitly if they are allowed to default.

Making a loop later (or something different than a loop) and populating the array with data is a second pass of assigning values to its elements (after initialization).

Sometimes the term "defining" an array is used to mean = declaration + allocation + initialization (It's been said this is not widely used in Java terminology)

// 2 elements having two string object refs
String s1[] = {"12","saf"};

// 2 elements holding 2 integer values
int s2[] = {1,2};

// 2 elements with no objects allocated (elements are null)
Object s3[] = new Object[2];

// 3 string objects 
String s4[][] = {{"12","2d"},{"saf"}};

// 4 string objects 
String s5[][] = {{"12","2d"},{"saf","fds"}};

/* declared array holds 2 arrays each can contain 2 objects 
   but they're not allocated so nested array elements are all null
 */
Object s6[] = {new Object[2],new Object[2]};
John K
Thanks you ,'jdk' for your expressive explanation
didxga
+1 - though I'd have preferred it if you have left out the "definition" of *defining*, since that is **not** widely recognized Java terminology. (If someone said to me "I have defined an array ...", I'd say please explain what you mean.)
Stephen C
@Stephen: I'm from the C# world so I wondered about the terms being off, but the array concepts are exactly the same. For the record and for no other reason than saying it, about the similarities and knowledge crossover, I do believe Microsoft lifted stuff from Java even though they say C++ was their implementation reference.
John K
`String s4[][] = {{"12","2d"},{"saf"}};` is not what I would call a "rectangular" array; I would call it "ragged". Java doesn't quite have rectangular arrays the way C or C# does. For example, in C# you could say `String[,] s5 = {{"12","2d"},{"saf","fds"}};` and it would only allocate a single block of memory, not 3 (assuming interned strings).
Gabe
@gabe: I removed terminology about jagged and rectangular.
John K