• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringBuffer Question.

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question:

1. StringBuffer s1 = new StringBuffer("abc");

2. StringBuffer s2 = s1;

3. StringBuffer s3 = new StringBuffer("abc");

How many objects are created ?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question. What i think is, that one object is created in the string pool, "abc". And then there are two Stringbuffer Objects on the heap. But i am not sure.

Gru�
J�rg
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes in fact I even think the same, There will be total 3 objects created any expert comments from anyone?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer does not have a pool like String. So i think the answer is 2.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 objects will be created, s1, s3 and the compile time String literal "abc"
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I found such kind of question in the real exam, should I consider even the object from string pool ?

I mean, the correct answer would be 3 ? (Two StringBuffers plus one String from pool ?)
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only two objects will be created.
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys

There seems no consensus on this question whether 2 objects will be created or 3 . Can any one please confirm correct choice.

Thanks
 
Amisha Shah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

i m also cofused between 2 and 3. i dont know , whether StringBuffer objects are placed in string pool or not. if so then there are 3 objects ane if not then 2 objects created.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Placing objects in pool happens only when we use literal assignments i.e. String s1="String" but not when we use "new" to create object i.e. String s1= new String("String")

Here there are total four objects created.
Two objects of class String Buffer. First referred by s1 ans s2. Second referred by s3.
Two objects of class String. Both having value "abc".

hth.
--Harshil
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Placing objects in pool happens only when we use literal assignments i.e. String s1="String" but not when we use "new" to create object i.e. String s1= new String("String")



So in other words if I say


It will create two objects i.e. first 'abc' in string pool and other s2. Am I right?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I think that a question that containig String objects and asking about how many objects created will not appear in the exams. Because it is related to the String pool topic.
And as a small comment to the subject i would like to add the following
  • here we are creating a StringBuffer objects, but by passing a compile time String literal to the string buffer the JVM will evaluate the "abc" expression and creates an object in the pool if it does not exist or it will locate the existing object in the pool and in either ways it will copy the object reference to the StringBuffer construtor which will be used internally to initialize the StringBuffer object.
  • Here you can't say exactly how many objects are created; take the following scenario

  • You create s1 (it envolves creating the "abc" pool object and the StringBuffer object, at this step we had created 2 objects but with only one referenced, s2 is only a reference to s1 object. when we create s3 it will check if the "abc" is found on the pool or not if its not found it will create a pool object otherwise it will use the already created object for s1. so here it may create 3 or 4 objects depending on the garbage collector used by the JVM.
    i hope this is helpfull for you folks.

    Saeed Labadi
    [ April 17, 2006: Message edited by: Saeed Labadi ]
     
    Ranch Hand
    Posts: 176
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    First an object of String is created with contents "abc"

    after it two String Buffer objects (in case 1 and 3)are created with content in above string object that is "abc"

    in case s2 no object is created we are just assigning reference of an object to a reference variable


    so a total of 3 object are created(two stringbuffer and 1 string)
     
    bnkiran kumar
    Ranch Hand
    Posts: 176
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    in case of case 3 i think no new string object is created as both has same content "abc"
     
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi

    two stringbuffer object created and one string object
     
    Ranch Hand
    Posts: 94
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well, in my humble opinion, after reading the Sierra+Bates a number of times, the String Constant Pool is a special area of memory, so we can infer that constants created on the pool are not objects in the heap.
    On the other hand, and again according to Sierra+Bates, when creating a new String object using the new keyword the literal string will be created in the heap and additionally the literal will be added to the String constant pool. So, from the initial question I would answer that the 2 StringBuffers are created in the heap, and the literal string wouldn't count.
    But that's only my humble opinion...
    Marx
     
    Marx Villegas
    Ranch Hand
    Posts: 94
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'd like to reply myself since I misunderstood what I read in tha Sierra/Bates. I just found this in a place not so far away:

    "Storage of Strings - The String Literal Pool
    If you've done any preparation for the SCJP exam (and quite possibly even if you haven't), you've probably heard of the "String Literal Pool." What is the String Literal Pool? Most often, I hear people say that it is a collection of String objects. Although that's close, it's not exactly correct. Really, it's a collection of references to String objects. Strings, even though they are immutable, are still objects like any other in Java. Objects are created on the heap and Strings are no exception. So, Strings that are part of the "String Literal Pool" still live on the heap, but they have references to them from the String Literal Pool."

    Check it all here:
    http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

    I guess there'll be three objects in the end...
    Marx
     
    Edisandro Bessa
    Ranch Hand
    Posts: 584
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi guys,

    For those who still are confused about this question :

    Objects in the String pool are never eligible for GC just because the pool has a valid reference for all these objects. So, in this question even though 3 objects were created (Two StringBuffers plus one String from pool), only two objects are eligible for GC.

    Objects from pool are never eligible for GC just because it's managed by pool and have always valid references for it.
     
    Marx Villegas
    Ranch Hand
    Posts: 94
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OK! That's good to know
    Thanx a lot
    XM
     
    Ranch Hand
    Posts: 135
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Edisando,

    I cannot understand by what you mean

    Objects from pool are never eligible for GC just because it's managed by pool and have always valid references for it.



    String s=new String("abc");

    It create two object one in heap memory and another in pool but the reference will point to heap memory object and the pool object has no reference.

    Plz clarify on this
     
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thx Marx Good Tutorial.
    Previously i know something abt it.
    Now i know almost everything CORRECTLY.

    Thx again,
    Naveen
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    class Test
    {
    public static void main(String args[]) {
    StringBuffer sb1 = new StringBuffer("iswasif");
    StringBuffer sb2 = sb1;
    StringBuffer sb3 = new StringBuffer("iswasif");
    if(sb1==sb2) {
    System.out.println("sb1 and sb2 are equal"); //References are same
    }
    if(sb2==sb3) {
    System.out.println("sb2 and sb3 are equal"); //References are different
    }
    }
    }

    Answer is 2 objects.

    If you look at the above example, you will come to know that how many objects were created in the heap.
    StringBuffer sb2 = sb1;
    This statement says that sb2 refer sb1, whatever object sb1 pointing the same object sb2 also pointing. That means, sb1 and sb2 are references which pointing the same objects.

    Second object is created because of the below statement
    StringBuffer sb3 = new StringBuffer("iswasif");
     
    Naveen Kollipara
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi All,

    i too think there r only two objects,that to StringBuffer

    The question of StringConstantPool comes when type is String as String is
    immutable .As StringBuffer is mutable i think there is no point of
    registering with StringPool,checking the pool while creating etc.

    What u people say ???

    Naveen
     
    reply
      Bookmark Topic Watch Topic
    • New Topic