• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

how many objects are created?

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
Answer given is: 3
Can somebody explain?
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1 references a StringBuffer (object #1)
s2 references same StringBuffer referenced by s1
s3 references a StringBuffer (object #2)
"abc" is created in the string pool (object #3)
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JUNILU LACAR:
s1 references a StringBuffer (object #1)
s2 references same StringBuffer referenced by s1
s3 references a StringBuffer (object #2)
"abc" is created in the string pool (object #3)


I thought object can be created through 'new' only, can you please explain why "abc" is an object and inthe code if we change
line 3 as StringBuffer s3 = new StringBuffer("xyz"); so will there now be 4 objects?
Thanks in advance
Jyotsna
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are objects, regardless of whether they are in the string pool (as a string literal) or in the heap (as an object created by new). See Dave Vick's post referencing the JLS in this thread: http://www.javaranch.com/ubb/Forum24/HTML/010491.html
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing that might convince you that string literals are objects is the fact that you can write this:
<pre>
void test( String s ) {
if ( "This is an Object".equals(s) )
System.out.println("It works!");
}
</pre>


[This message has been edited by JUNILU LACAR (edited June 28, 2001).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu,
According to me only two objects are created one by s1 & another by s2.Whereas s2 is referencing the same object as s1.
Can you explain more that how 3 objects are created.
Regards,
Hassan.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hassan,
Kindly hit your browser's refresh button so you can see the previous replies above which I hope will clearly explain why 3 objects are created, not 2.
 
Hassan Naqvi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu,
Now no need for pressing refresh button.The link you give defines well & deeply..
Regards,
Hassan.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic