aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer Question." Watch "StringBuffer Question." New topic
Author

StringBuffer Question.

Amisha Shah
Ranch Hand

Joined: Mar 03, 2006
Posts: 33
Question:

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

2. StringBuffer s2 = s1;

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

How many objects are created ?


Amisha Shah.<br />SCJP 1.4
Joerg Waldmann
Greenhorn

Joined: Mar 14, 2006
Posts: 17
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
Sanjeev Sarkar
Greenhorn

Joined: Mar 14, 2006
Posts: 1
Yes in fact I even think the same, There will be total 3 objects created any expert comments from anyone?


Sanjeev Sarkar<br />Java Developer
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

StringBuffer does not have a pool like String. So i think the answer is 2.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Rupak Khurana
Ranch Hand

Joined: Mar 01, 2005
Posts: 89
3 objects will be created, s1, s3 and the compile time String literal "abc"


SCJP 1.5<br />SCWCD 1.4
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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 ?)


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
vivek yadav
Greenhorn

Joined: Apr 16, 2006
Posts: 8
only two objects will be created.


Vivek Kumar Yadav
SCJP, SCWCD
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
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


"Know where to find the solution and how to use it - that's the secret of success."
Amisha Shah
Ranch Hand

Joined: Mar 03, 2006
Posts: 33
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.
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
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

Joined: Nov 14, 2005
Posts: 324
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?
Saeed Labadi
Greenhorn

Joined: Mar 30, 2006
Posts: 3
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 ]
    bnkiran kumar
    Ranch Hand

    Joined: Mar 02, 2006
    Posts: 176
    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)


    Kiran Kumar.
    bnkiran kumar
    Ranch Hand

    Joined: Mar 02, 2006
    Posts: 176
    in case of case 3 i think no new string object is created as both has same content "abc"
    mohamed ewis
    Greenhorn

    Joined: Feb 05, 2006
    Posts: 18
    hi

    two stringbuffer object created and one string object
    Marx Villegas
    Ranch Hand

    Joined: Mar 10, 2006
    Posts: 94
    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


    When the compiler's not happy, ain't nobody happy...
    Marx Villegas
    Ranch Hand

    Joined: Mar 10, 2006
    Posts: 94
    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

    Joined: Jan 19, 2006
    Posts: 584
    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

    Joined: Mar 10, 2006
    Posts: 94
    OK! That's good to know
    Thanx a lot
    XM
    gaurav singhal
    Ranch Hand

    Joined: Nov 18, 2005
    Posts: 135
    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
    Naveen Kollipara
    Ranch Hand

    Joined: Feb 20, 2006
    Posts: 32
    Thx Marx Good Tutorial.
    Previously i know something abt it.
    Now i know almost everything CORRECTLY.

    Thx again,
    Naveen


    Regards,<br />Naveen<br />SCJP 5 - 83%<br />SCWCD - 84%
    P.Rajesh
    Greenhorn

    Joined: Apr 21, 2006
    Posts: 1
    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

    Joined: Feb 20, 2006
    Posts: 32
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: StringBuffer Question.