Author Topic:   Array Initialization
Supree
greenhorn
posted February 28, 2000 08:32 AM             
Hi,
I got this one in a Mock exam.

Which of the following code correcly creates an array of
four initialized string objects.

a. String players[] = new String[4];
b. String players[] = {"","","",""};
c. String players[];
players = new String[4];


Author answer is b.

I disagree.
I tested it with == operator comparing players[0] and players[1] and the result is they are equal. So they all refer to only one object.
Infact none of the above statements creates array of
four string objects.

Any comments please.


------------------

maha anna
bartender
posted February 28, 2000 09:03 AM             
a. String players[] = new String[4];
b. String players[] = {"","","",""};
c. String players[]; players = new String[4];
opt a:
It creates a String array. Java inits all the elements of array of objects to null by default.So each element in the String array is initialized to null. But these each String object references players[0],players[1],palyers[2],players[3] are not yet allocated to any String objects.
Note : players , players[0],players[1],players[2],players[3] are all refs. We have to explicitly create new String objects and assign these players[n] ref to these new refs.
players ----->[null (players[0]) ] (not initialized)
[null (players[1]) ] (not initialized)
[null (players[2]) ] (not initialized)
[null (players[3]) ] (not initialized)
opt c:
same as opt a.
opt b:

players ----->[(players[0]) ] players[0]-----> " "
[(players[1]) ] players[1]-----> " "
[(players[2]) ] players[2]-----> " "
[(players[3]) ] players[3]-----> " "

See here all elements of the String array players is initialized to the String object "" (This "" object is in String pool)
I am in a hurry. I hope this helps. So ONLY ans b is correct.
regds
maha anna

[This message has been edited by maha anna (edited February 28, 2000).]

Dave Brookes
greenhorn
posted February 28, 2000 09:14 AM             
players[0]== players[1]

reveals that both elements of the array contain the same object, namely "".

If you replace all the "" with "a" it gives the same result. If you put a "b" in players[1] the equality test fails but the array remains initialised ok.

------------------
Regards,
Dave

Jim Yingst
sheriff
posted February 28, 2000 09:59 AM             
Dave- you're right. Because of the way Java handles String literals, all the "" occurences in the code end up referring to the same object instance. (Since String is immutable, this creates no problems, and is more efficient than creating four identical strings.) There is no significant difference between the two arrays - only a semantic difference in describing how they were created. The array in B has been initialized; the array in A has not - it only received Java's default values. Initialization is an explicit assignment statement, which is required by the compiler in certain cases. Because the question explicitly asked for an array that had been initialized, B is correct and A is not - however it's such a nit-picky point that I think it's just a bad question.

Supree
greenhorn
posted February 28, 2000 10:03 AM             
Hi Maha Anna,
My point is option B creates only one string object "" placed in the pool and all the array references pointing to the same string object.

It does not create four string objects.
Is it okay?
thanks,
Supree

maha anna
bartender
posted February 28, 2000 02:01 PM             
Supree,
Yes.Your point is correct.Why I went into details was, this point 'there is only one String "" object exists in the String pool and all the array elements refer to this pool object' is not related to answering the question.The question asked to choose the answers which 'correcly creates an array of four initialized string objects'.Since you started answering with the statement I disagree , I thought you don't agree that the answer b is correct.
Anyhow we all agree that the ONLY correct answer is option b.
regds
maha anna

[This message has been edited by maha anna (edited February 28, 2000).]

sree
unregistered
posted February 28, 2000 02:34 PM           
I declare a String array like
String names[]={"a","b","c"};
Does it mean that a,b and c are String literals and are created in the String pool and there fore not elligible for "GC".

Like if i set String names[1]=null will "b" elligible for "GC" or is it different when we are dealing with "Arrays".


maha anna
bartender
posted February 28, 2000 03:38 PM             
yes.They are Not elgible for GC. It is NOT different when we are dealing with arrays.

String names[]={"a","b","c"}; // "a","b","c" are String literals and they are in String pool. So they are NOT eligible for GC. But Tony again and again insists that all GC related questions in SCJP2 Exam will be asked with objects other than literals in order to avoid the confusion about whether the String pool objects are Garbage Collected or not.
regds
maha anna

I declare a String array like
String names[]={"a","b","c"};
Does it mean that a,b and c are String literals and are created in the String pool and there fore not elligible for "GC".

[This message has been edited by maha anna (edited February 28, 2000).]

sree
unregistered
posted February 28, 2000 07:54 PM           
Thanks Maha Anna.

Ramana Namuduri
greenhorn
posted March 04, 2000 02:32 PM             
Let me get back the question to all of you again.
My answer is all the three.
Now, if any variable is not initialized and is referred in a method, the compiler complaints for"variable referred before it is initialized". That means, the variable must be initialized or it must hold some value before it has been referred.Either it can be explicitly initialized(assignment operation) or it can be implicitly initialized(using new). This problem does not exist for an instance variable. Because, they get initialized automatically. But, when any one of the given answers is used for string varibles and directly referred any where in the method, the compiler does not give any error, means, initialization is automatically done.

maha anna
bartender
posted March 05, 2000 11:14 AM             
Ramana,
The qstn here is Which of the following code correcly creates an array of four initialized string objects. In case 1 and 3 the array elements are initialized by default to 'null'.(Since this is an an array of Objects) But only ans 2 initializes the array with String objects. So only ans 2 is correct.
regds
maha anna

[This message has been edited by maha anna (edited March 05, 2000).]

|