This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String and StringBuffer" Watch "String and StringBuffer" New topic

String and StringBuffer

swarna kumar

Joined: Nov 11, 2001
Posts: 17
i came across this ques in RHE book,
String s="abcde";
StringBuffer s1=new StringBuffer("abcde");
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
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.

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...

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

Joined: Jan 23, 2001
Posts: 112

where can i find instanceof in API
pls help

please use the [code][/code] tags when showing code. visit <a href="" target="_blank" rel="nofollow"></a> ,for more details
Valentin Crettaz
Gold Digger

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 ]

[Blog] [Blogroll] [My Reviews] My Linked In
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
thank you Val
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: String and StringBuffer
It's not a secret anymore!