Results 1 to 7 of 7
Thread: StringBuffer doubt
- 06-21-2012, 08:30 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
- 06-21-2012, 09:18 AM #2
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
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
- 06-21-2012, 09:30 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
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.Java Code:StringBuffer whiteboard1 = new StringBuffer("foo"); StringBuffer whiteboard2 = new StringBuffer("foo");
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.
- 06-21-2012, 09:41 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: StringBuffer doubt
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.)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
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.
- 06-21-2012, 10:22 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
- 06-21-2012, 10:33 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: StringBuffer doubt
You're welcome.
- 06-21-2012, 10:35 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
Similar Threads
-
StringBuffer
By Dayanand in forum New To JavaReplies: 9Last Post: 03-01-2011, 10:04 AM -
How to use a StringBuffer?
By Hallowed in forum New To JavaReplies: 5Last Post: 02-18-2011, 05:38 AM -
How to do this with StringBuffer???
By mlibot in forum New To JavaReplies: 2Last Post: 03-24-2010, 12:40 AM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum Advanced JavaReplies: 3Last Post: 03-04-2009, 06:03 AM -
java.lang.StringBuffer.append(StringBuffer.java(Co mpiled Code))
By Ashok Dave in forum New To JavaReplies: 1Last Post: 03-03-2009, 05:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks