June 2018
Beginner
722 pages
18h 47m
English
We talked about the equals() method implementation extensively, but always assumed that it was invoked on a non-null object, obj, so the call obj.equals(anotherObject) could not generate NullPointerException.
Yet, sometimes we need to compare two objects, a and b, when one or both of them can be null. Here is typical code for such a case:
boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b));}
This is the actual source code of the boolean Objects.equals(Object a, Object b) method. It allows comparing two objects using the method equals(Object) and handles cases where one or both of them are null.
Another related method of the class Objects is boolean deepEquals(Object a, Object b). Here ...
Read now
Unlock full access