Methods Common to All Objects

Item 0: Obey the general contract when overriding equals

Equals:
Must satisfy following Criteria:

  1. 1.Reflexive(you can use short circuit technique like if this==obj return true)
  2. 2.transitive
  3. 3.symmetric
  4. 4.Non-Nullity
  5. 5.Consistency

Frequently used operators :

== vs Equals

Note: == check for reference equality where as Equals checcks for content in object equality
e.g.
String a1=new String(“HELLO”);
String a2=new String(“HELLO”);
System.out.println(a1==a2);
System.out.println(a1.equals(a2));
Output:
flase ..becasue a1 and a2 both points to differen memory location
true..because a1 and a2 contents are same.

==

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment