Results 1 to 10 of 10

Thread: StringBuffer

  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default StringBuffer

    package mutable;

    public class Expt
    {
    public static void main(String[] args)
    {
    StringBuffer sb = new StringBuffer();
    sb.append("abc");
    sb.append("xyz");

    StringBuffer sb1 = sb.append("123");
    System.out.println("sb1->"+sb1);
    System.out.println("sb->"+sb);
    System.out.println(sb==sb1);
    System.out.println(""+sb==""+sb1);
    }
    }

    output
    sb1->abcxyz123
    sb->abcxyz123
    true
    false


    what makes the difference between the below to statements
    System.out.println(sb==sb1);
    System.out.println(""+sb==""+sb1);


    why (" "+sb==" "+sb1) is false

    Thanks in advance
    Daya
    Last edited by Dayanand; 03-01-2011 at 08:55 AM. Reason: mistakes

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    You are comparing references not values with == and in the second one new objects are being creted due to the string concatenation and these new objects have different reference values even though the actual string values are the same.

    IOW do not use == to compare objects (of any kind) unless you wish to see if those variables reference the exact same object. If you wish to compare the values of the objects behind those references use the equals method.

  3. #3
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    thanks for your valuable solution

    one more doubt
    but i think no new objects will be created in case of StringBuffer,has StringBuffer provides Mutable objects
    can u please help me out in this case.?
    Last edited by Dayanand; 03-01-2011 at 09:14 AM. Reason: clearity

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Doesn't matter. String concatenation (which is what that + causes in that form) produces Strings not StringBuffers.

    Edit: And, it doesn't really matter what that + produces, nor does it matter that mutable objects were fed into it, it produces new objects.
    Last edited by masijade; 03-01-2011 at 09:22 AM.

  5. #5
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    sorry Sir,for asking u again and again.!!!!!!!!!!
    System.out.println(""+sb==""+sb1);
    ""+sb will provide one string
    ""+sb1 will provide one string

    but at last sb & sb1 strings will have same content, but why it results in false when sb==sb1 is checked

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by Dayanand View Post
    sorry Sir,for asking u again and again.!!!!!!!!!!
    System.out.println(""+sb==""+sb1);
    ""+sb will provide one string
    ""+sb1 will provide one string

    but at last sb & sb1 strings will have same content, but why it results in false when sb==sb1 is checked
    Because they are different objects. I.E. they are represented by different structures on the heap even though those structures contain the same value they are still different structures and so have different "addresses" and == compares these "addresses" not the values of the structures behind those "addresses".

  7. #7
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Because they are different objects. I.E. they are represented by different structures on the heap even though those structures contain the same value they are still different structures and so have different "addresses" and == compares these "addresses" not the values of the structures behind those "addresses".
    System.out.println(""+sb.equals(""+sb1));
    out-put
    false

    has equals() method will check content then why (""+sb.equals(""+sb1)) results in false

  8. #8
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    as already mentioned, when you compare two objects with == you are comparing its references and not the content.
    System.out.println(""+sb.equals(""+sb1));
    out-put
    false

    has equals() method will check content then why (""+sb.equals(""+sb1)) results in false

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Because you are comparing sb to ("" + sb1) and prepending "" to that result, you are not comparing ("" + sb) to ("" + sb1)
    Java Code:
    (""+sb).equals(""+sb1)

  10. #10
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Because you are comparing sb to ("" + sb1) and prepending "" to that result, you are not comparing ("" + sb) to ("" + sb1)
    Java Code:
    (""+sb).equals(""+sb1)
    very very very superb
    really i am very thankful for spending your valuable time for my doubts

    Thanks a lot Sir
    Daya

Similar Threads

  1. How to use a StringBuffer?
    By Hallowed in forum New To Java
    Replies: 5
    Last Post: 02-18-2011, 05:38 AM
  2. How to do this with StringBuffer???
    By mlibot in forum New To Java
    Replies: 2
    Last Post: 03-24-2010, 12:40 AM
  3. Replies: 3
    Last Post: 03-04-2009, 06:03 AM
  4. Replies: 1
    Last Post: 03-03-2009, 05:27 AM
  5. Should I use StringBuffer?
    By GenkiSudo in forum New To Java
    Replies: 7
    Last Post: 09-21-2008, 12:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •