• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

RHE Qs

 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The objects are created by 'new' so there should be only 2. There are 3 references to them. Don't know why the answer should be three !
Deepti
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification.
I now feel more cofident/comfortable to choose c as the answer.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
This added doubt in me.
Is the the answer c or d ?
Please be clear.
-Tony.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


My thought:
there are two objects created with 3 references,
among them 2 references point to the same sb object.
victor
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jose!
Could you please explain your answer in the context of the question. I could not understand your explanation. Why is the answer 'd' ie how is it that 3 objects are created?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason there are three objects is because two are created by the stringbuffer using the new operator and the third is the two strings. String are special objects in java the java run time environment maintains a pool of string objects.
So for example if you have
String s = "hello";
String j = "hello";
So j == s will return true; ( Reference checking is done)
The runtime environment basically created only one object and shared that objects with ref s and ref j. But if you have done
String s = new String("Hello");
String j = new String("Hello");
You have created two seperate String objects and if you now do
j==s ( it will return you false);
Now since Strings are immutable, if you try to manipulate this string object a new string object will be given to you.
But back to the original poster's question
if the question had something like
StringBuffer sb = new StringBuffer(new String("") );
StringBuffer sb2 = new StringBuffer( new String(""));
StringBuffer sb3 = sb;
All you have now done is created two StringBuffer Object and two new String Object.
For the StringBuffer sb3 all you have done is now made the refernce sb3 point to wherever reference sb is pointing. So if you do any manipulation like append or .... on sb3 it will also be reflected on sb.
Hope this helps!!!
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi chichih,
Just to give you the short-hand answer as to how many objects:
1 -> String literal "abc"
2 -> StringBuffer pointed to by s1 and s2
3 -> StringBuffer pointed to by s3
Regards,
Manfred.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]


Jose,
Ans is c, not d.
If you run the above code, you will find
s1 and s2 point to the same object.
victor
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
Thanks for all of your inputs & explanations.
regards
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are already 10 posts. Can somebody, like a senior guy or say, a bartender please answer this. Please do not take any offense on this. The point is to know exactly the right answer. There are no marks for half answered questions, and each mark counts.

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

 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I confirm what Manfred and Jose said: 3 objects are created, that is, 2 StringBuffers and 1 String. So the answer is d.
HIH
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


Ok, after rereading you guys' posts, I now
understand what is happening. The ans should be 3 like Jose said since there is another String object created by the literal. Cheers.


victor
[ January 15, 2002: Message edited by: victor gu ]
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHERE is the String literal here ? they are all StringBuffers ! I am sorry but i did not get this String literal.
String literals are created by class "String". and class "StringBuffer" is a separate class. In the question there is no String class.
again sorry for prolonging the thread.

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.

 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



Conceptually, you can think of the above sequence of statements as being equivalent to the following:
char[] value = {'a', 'b', 'c'}; //creates a char array of size 3 and initializes it with this list
- - - - - - - - - - - - - - - - - - - - - - - - -
String abcLiteral = new String(value); //FIRST new object

1. StringBuffer s1 = new StringBuffer(abcLiteral); //SECOND new object
2. StringBuffer s2 = s1 ; //no new object, only reference copy
3. StringBuffer s3 = new StringBuffer(abcLiteral); //THIRD new object.
Rob

[ January 15, 2002: Message edited by: Rob Ross ]
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so basically as i see this question, one got to differentiate between objects and object references. right ??
because s1, s2, s3 are object references. (references pointing to objects). And the question is asking "how many objects" ? right ?
for this we need to find how many actual objects have been created ? right ?
is my thinking ok ?
please comment as this is an important concept.

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.

 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


so basically as i see this question, one got to differentiate between objects and object references. right ??


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 ]
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks mucho mucho.

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 ]

 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!