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