Results 1 to 7 of 7
  1. #1
    saiganesh44 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default StringBuffer doubt

    Hi,

    Got a doubt in StringBuffer class.

    Why doesn't it implement the equals method like String class does?

  2. #2
    Jeeva7 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: StringBuffer doubt

    Hi

    The purpose of String and StringBuffer are different.
    StringBuffer doesn't extends String, if it extends then we can expect the same behavior
    Both extends Object class

    verify java API
    String class implements comparable class so u can use equals method
    StringBuffer doesn't extend comparable class so no point in talking with comparing two stringBuffer object like string

    String every time creates new object and it replaces the new one with the old one -> waste of memory
    String buffer doesn't create any new object it modifies the existing one -> no waster of memory

    Thanks
    Jeeva

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: StringBuffer doubt

    Both classes respect the documented behaviour of equals(), so to that extent, they're the same.

    Exactly what equals() does is up to whoever writes the class (as with any method) and I imagine they want the methods to be expressive and useful.

    Java Code:
    StringBuffer whiteboard1 = new StringBuffer("foo");
    StringBuffer whiteboard2 = new StringBuffer("foo");
    So, I have two different whiteboards with the same thing written on them. And I can express precisely that with Java: different (nonequal) whiteboards, but the same (equal) string contents.

    Personally I find the choice of implementation here quite natural because I wouldn't regard whiteboards as being equal in any significant sense just because they happen to have the same word written on them.

    Whiteboards are mutable in the sense that they can change their contents - a teacher can erase the contents and reuse the same whiteboard where "the same" means "the whiteboard in such-and-such a room". The student, on the other hand, wants to be careful and copy exactly what the teacher has written where "exactly" has the meaning "the same characters in the same order". In ordinary life we use both semantics when dealing with written language.

    Nothing is lost with this different semantics for whiteboards (with its stress on the container rather than the contained) because if I want to I can evaluate

    Java Code:
    whiteboard1.toString().equals(whiteboard2.toString())
    Last edited by pbrockway2; 06-21-2012 at 09:44 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: StringBuffer doubt

    verify java API
    String class implements comparable class so u can use equals method
    StringBuffer doesn't extend comparable class so no point in talking with comparing two stringBuffer object like string
    In fact equals() is declared in the Object super class, so it is a method-in-good-standing of both String and StringBuffer. That String implements the Comparable interface is largely irrelevant (but see the comments at the start of the API docs of that interface for the conditions that String's implementation of equals() should - and does - meet.)

    It does make sense to compare StringBuffer instances for equality (and whiteboard1.equals(whiteboard2) works fine): just not on the basis of what their contents happen to be at some instant.

  5. #5
    saiganesh44 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: StringBuffer doubt

    Quote Originally Posted by pbrockway2 View Post
    Both classes respect the documented behaviour of equals(), so to that extent, they're the same.

    Exactly what equals() does is up to whoever writes the class (as with any method) and I imagine they want the methods to be expressive and useful.

    Java Code:
    StringBuffer whiteboard1 = new StringBuffer("foo");
    StringBuffer whiteboard2 = new StringBuffer("foo");
    So, I have two different whiteboards with the same thing written on them. And I can express precisely that with Java: different (nonequal) whiteboards, but the same (equal) string contents.

    Personally I find the choice of implementation here quite natural because I wouldn't regard whiteboards as being equal in any significant sense just because they happen to have the same word written on them.

    Whiteboards are mutable in the sense that they can change their contents - a teacher can erase the contents and reuse the same whiteboard where "the same" means "the whiteboard in such-and-such a room". The student, on the other hand, wants to be careful and copy exactly what the teacher has written where "exactly" has the meaning "the same characters in the same order". In ordinary life we use both semantics when dealing with written language.

    Nothing is lost with this different semantics for whiteboards (with its stress on the container rather than the contained) because if I want to I can evaluate

    Java Code:
    whiteboard1.toString().equals(whiteboard2.toString())
    Thanks a lot, that explained well.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: StringBuffer doubt

    You're welcome.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,047
    Rep Power
    20

    Default Re: StringBuffer doubt

    I suspect part of it is also to discourage people from simply using StringBuffer/Builder in place of Strings.
    You may think this is unlikely, but I've seen more than one person put this forward so that they could change String values "easily".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. StringBuffer
    By Dayanand in forum New To Java
    Replies: 9
    Last Post: 03-01-2011, 10:04 AM
  2. How to use a StringBuffer?
    By Hallowed in forum New To Java
    Replies: 5
    Last Post: 02-18-2011, 05:38 AM
  3. How to do this with StringBuffer???
    By mlibot in forum New To Java
    Replies: 2
    Last Post: 03-24-2010, 12:40 AM
  4. Replies: 3
    Last Post: 03-04-2009, 06:03 AM
  5. Replies: 1
    Last Post: 03-03-2009, 05:27 AM

Posting Permissions

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