i generating equals methods , wondering if advisable use objects.equals() method compare fields java 7.
eclipse generates equals this:
public class { private string a; private string b; @override public boolean equals(object obj) { if(this == obj) return true; if(obj == null) return false; if(getclass() != obj.getclass()) return false; other = (a)obj; if(a == null) { if(other.a != null) return false; } else if(!a.equals(other.a)) return false; if(b == null) { if(other.b != null) return false; } else if(!b.equals(other.b)) return false; return true; } }
and wondering, if practice:
public class { private string a; private string b; @override public boolean equals(object obj) { if(this == obj) return true; if(obj == null) return false; if(getclass() != obj.getclass()) return false; other = (a)obj; return objects.equals(a, other.a) && objects.equals(b, other.b); } }
what think? tried test performance of it, showed no difference..
yes, it's practice. way equals
method looks clearer without drawbacks except additional method call in cases inlined jit compiler. please note objects.equals
appeared first in java 7, not java 8.
Comments
Post a Comment