File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == & equals( ) in StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== & equals( ) in StringBuffer" Watch "== & equals( ) in StringBuffer" New topic
Author

== & equals( ) in StringBuffer

sadas dsdasd
Greenhorn

Joined: Dec 14, 2002
Posts: 13
Hi all,
StringBuffer sb1=new StringBuffer("abc");
StringBuffer sb2=new StringBuffer("abc");
if(sb1.equals(sb2))
I think it returns false as StringBuffer doesn't override equals() method.If I am wrong please correct me.
What is the result if it is like this.
StringBuffer sb1=new StringBuffer("abc");
StringBuffer sb2=sb1;
what does the following comparisons return?
1)if(sb1==sb2)
2)if(sb1.equals(sb2))
please help me.Thanks in advance.
Praveen Zala
Ranch Hand

Joined: Jul 02, 2000
Posts: 118
The answer to the first Question is correct !!
The answer to 2a) is it returns true as there is only one
reference at sb1 !!
The answer to 2b) is also true as it is the same stringbuffer
object
Praveen
sadas dsdasd
Greenhorn

Joined: Dec 14, 2002
Posts: 13
Thanks.
Q.StringBuffer sb1=new StringBuffer("abc");
StringBuffer sb2=new StringBuffer("abc");
What does this return?
if(sb1==sb2)
Q.String s1=new String("abc");
String s2=s1;
what does the following comparisons return?
A.if(s1==s2)
B.if(s1.equals(s2))
please give me the answer.Thanks in advance.
sadas dsdasd
Greenhorn

Joined: Dec 14, 2002
Posts: 13
please help
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The answer to the first one is false because sb1 and sb2 are two different references.
s1 == s2 is true since both refer to the same reference.
s1.equals(s2) is true because if the two string references are same then this has to be true.
sadas dsdasd
Greenhorn

Joined: Dec 14, 2002
Posts: 13
Thanks a lot carl.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == & equals( ) in StringBuffer
 
Similar Threads
StringBuffer equals()
String and StringBuffer
StringBuffer
equals() in StringBuffers
string buffer