Which of the following correctly fills in the blank so this code compiles and prints true?
public class Main { private String text; public int hashCode() { return text.hashCode(); } //from w w w . ja v a 2 s. c om public boolean equals(Object o) { if ( ) return false; Main b = (Main) o; return text.equals(b.text); } public static void main(String[] args) { Main b1 = new Main(); Main b2 = new Main(); b1.text = "mickey"; b2.text = "mickey"; System.out.println(b1.equals(b2)); } }
C.
There is no instanceOf keyword, making Options B and D incorrect.
There is an instanceof keyword.
If an object is the wrong type, the equals()
method should return false, making Option C the answer.