Cheatsheet

Cheatsheet

java.util.Objects #

MethodDescription
equals(Object a, Object b)Safely compares two objects for equality.
deepEquals(Object a, Object b)Performs a deep comparison of two objects, including arrays.
hash(Object... values)Generates a hash code for multiple objects.
hashCode(Object o)Returns the hash code of an object or 0 if null.
toString(Object o)Returns the toString() of an object or "null".
toString(Object o, String nullDefault)Returns the toString() of an object or a default string.
requireNonNull(T obj)Throws NullPointerException if the object is null.
requireNonNullElse(T obj, T defaultObj)Returns the object if non-null, or a default value.
isNull(Object obj)Checks if an object is null.
nonNull(Object obj)Checks if an object is non-null.
compare(T a, T b, Comparator<? super T> c)Compares two objects using a comparator.
checkIndex(int index, int length)Ensures that the index is within valid bounds.

Exceptions #

ExceptionTypeDescription
NullPointerExceptionUncheckedAttempt to use null as if it were an object.
ArrayIndexOutOfBoundsExceptionUncheckedAccessing an array with an illegal index.
IllegalArgumentExceptionUncheckedMethod argument is inappropriate or illegal.
IllegalStateExceptionUncheckedMethod called at an inappropriate time.
IndexOutOfBoundsExceptionUncheckedGeneral index is out of range.
ArithmeticExceptionUncheckedArithmetic error (e.g., divide by zero).
ClassCastExceptionUncheckedInvalid casting of an object.
NumberFormatExceptionUncheckedFailure to convert string to a number.
IOExceptionCheckedI/O operation failure.
FileNotFoundExceptionCheckedFile operation on a file that doesn’t exist.
InterruptedExceptionCheckedThread is interrupted while it is waiting or sleeping.
SQLExceptionCheckedDatabase access or SQL error.
NoSuchElementExceptionUncheckedAttempting to access an element that does not exist.
UnsupportedOperationExceptionUncheckedOperation is unsupported.
TimeoutExceptionCheckedOperation times out.
OutOfMemoryErrorErrorJVM runs out of memory (serious issue).