aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String and StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


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

String and StringBuffer

swarna kumar
Greenhorn

Joined: Nov 11, 2001
Posts: 17
i came across this ques in RHE book,
code:
String s="abcde";
StringBuffer s1=new StringBuffer("abcde");
if(s.equals(s1))
s1=null;
if(s1.equals(s))
s=null;
the compilation succeds,
actually, we can't compare String to a StringBuffer. If we do, it would return false,
then how the compilation succeds without giving any exception.
let me know this
swarna
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Compilation succeeds because the equals method has the following signature:

It has this signature because it is inherited from the Object class. Therefore, you can pass any object to the equals method.
However, the first check that is done is to make sure that the object passed in is of the same type as the object, like this:

If this check fails, false is returned. Therefore, it is legal to compare a String to a StringBuffer, but the equals method will always return false because they are of differing types.
Corey


SCJP Tipline, etc.
John Wolf
Ranch Hand

Joined: Feb 18, 2002
Posts: 64
But you should keep in mind that it will return true if you use toString() with StringBuffer. Eg.

Just an fyi...
Cheers!~
Sumit


SCJP 1.2, SCJP 1.4, SCJA, SCWCD 1.4<br /> <br />------------------------------<br />
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
hi,

where can i find instanceof in API
pls help


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The APIs are for methods, classes and packages descriptions.
instanceof is a keyword and can thus be found in JLS 15.20.2 Type Comparison Operator instanceof
[ July 24, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
thank you Val
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String and StringBuffer