wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer" Watch "StringBuffer" New topic
Author

StringBuffer

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi
What is the result of the following code:
StringBuffer sb1="ab";
StringBuffer sb2=new StringBuffer("ab");
1.sb1==sb2
2.sb1.equals(sb2)
I think both are false.If I am wrong please correct me.Thanks
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
I think that both are false too.
1) == returns true if both are initialize with literals and both are identical literal strings.
2) The equals method isn�t implemented in StringBuffer

[This message has been edited by Marcela Blei (edited July 21, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Are you sure about StringBuffer sb1="ab";
I guess it will not compile. The error will be incompatible type case.
Yes StringBuffer did not override the equals() method and so it will not compare the contents. It will behave just like ==.
Hope it helps!
Praveen Zala
Ranch Hand

Joined: Jul 02, 2000
Posts: 118
Hi,
Yes StringBuffer sb = "ab" is wrong and shud be
StringBuffer sb = new StringBuffer("ab");
Is this the only method of initializing StringBuffer ?
Praveen Zala
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Praveen,
You an also create an empty StringBuffer using the StringBuffer() or StringBuffer( length ) constructors and use various flavours of append() methods to initialize it.
ie., StringBuffer s = new StringBuffer("Hello") is same as

StringBuffer s = new StringBuffer() ;
s.append("Hello");
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: StringBuffer
 
Similar Threads
Doubt regarding StringBuffer
string and string buffers
== & equals( ) in StringBuffer
StringBuffer equals()
in what condition the equls to compare whether the two are the same object?