not so smart guy still curious to learn new stuff every now and then
not so smart guy still curious to learn new stuff every now and then
Originally posted by chichih Lin:
Could anyone advise why the answer is d ?
How many objects are created by the following code ?
1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBUffer("abc");
a. None
b. 1
c. 2
d. 3
thanks
SCJP2. Please Indent your code using UBB Code
Bos Indicus
Originally posted by Jose Botella:
String literals automatically create an string object unless it already exits in the string pool.
In a Java application there is a pool of String objects that are created either with a string literal or String.intern() method.
Each time either occurs before creating the string object the pool is checked to look for the existance of a string object with exactly the same content. If it is found no new string object is created. A reference to that string already in the pool is returned by intern or the literal string expression. If it doesn't exist it is created, added to the pool and a reference to it is returned.
Doing so the pool contains no duplicate string objects.
The reason for this is to speed the coomparation of string objects. If many of them should be done the programmer can "intern" the strings to "place" (only on copy of each) them in the pool. Now the comparations can be done with == , which is much more quicker than equals.
Ans is d.
[ January 14, 2002: Message edited by: Jose Botella ]
not so smart guy still curious to learn new stuff every now and then
Originally posted by chichih Lin:
Could anyone advise why the answer is d ?
How many objects are created by the following code ?
1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBUffer("abc");
a. None
b. 1
c. 2
d. 3
thanks
Originally posted by Valentin Crettaz:
I confirm what Manfred and Jose said: 3 objects are created, that is, 2 StringBuffers and 1 String. So the answer is d.
HIH
Originally posted by Manfred Leonhardt:
To All,
I gave you the correct answer with full explanation.
Victor is correct in that s1 and s2 point to the same object, but as I already stated in my correct answer above, he is forgetting about the String literal: "abc" which is also an object that is created by the compiler. What amazes me is that Victor uses the explanation of the literal string from Jose to somehow decide that the string literal doesn't exist!
NOTE: I am a bartender for Applets. Bartenders only appear as bartenders in their respective forums.
Regards,
Manfred.
Originally posted by chichih Lin:
Could anyone advise why the answer is d ?
How many objects are created by the following code ?
1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBUffer("abc");
a. None
b. 1
c. 2
d. 3
thanks
Rob
SCJP 1.4
Originally posted by Manfred Leonhardt:
Hi Mark,
String literals are created during the compile. Any time a literal string (any string enclosed in double quotes) is found and it is unique it is added into a String table. In the example code the string literal is "abc". Which is given as a parameter into 2 StringBuffer constructors.
Regards,
Manfred.
so basically as i see this question, one got to differentiate between objects and object references. right ??
Rob
SCJP 1.4
Originally posted by Rob Ross:
Yes, that is exactly what the question is asking about, if you understand the difference between an object and an object reference.
Object o1 = new Object();
Object o2 = o1;
Here, we have created ONE Object. We have declared TWO Object references. Both object references point to the same object. But we have only created one object.
Rob
[ January 17, 2002: Message edited by: Rob Ross ]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |