This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
1. The output is false, because according to JLS: "The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this String object." However, in your program s2 is not a String object. Hence the result is false. 2. == test gives an error, beacuse accroding to Khalid Mughal's book: To apply ==, the operands must be type compatible:it must be possible to cast one into the other's type. Otherwise it is a complile time error. hope this helps
Hi Nishesh! You should check the documentation properly, because there is no method "equals()" in StringBuffer class. But u wont get error because Object class' method "equals()" called, but it takes both operands as objects that means check their references only if match "true" return, else "false". try to compile and run this code: StringBuffer sb1 = new StringBuffer("ab"); StringBuffer sb2 = sb1; System.out.println(sb1.equals(sb2)); i think u got it Bye! Farhan
Hi Everyone, Farhan has rightly pointed out StringBuffer does not overrides the <code>equals()</code> of Object class. Before I explain the mechanism in the example of Nishesh. I would like to quote API pertaining to equals method of String class :
equals public boolean equals(Object anObject) Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Parameters: anObject - the object to compare this String against. Returns: true if the String are equal; false otherwise. Overrides: equals in class Object
On the contrary in case (2) ,
Hope you are clear now. Ravindra Mohan
[This message has been edited by Ravindra Mohan (edited May 15, 2001).]