Objects simply contains a set of utility methods that are useful in combination with Object instances. Note that it can't be instantiated (it's final and it has no public constructor) and only contains static methods.
The naming schema of putting utility methods in pluralized-name classes is pretty common in the JDK:
CollectionsArrays(although strictly speaking there is no correspondingArrayclass)- ...
Other libraries also use this scheme, for example Guava:
MapsStrings- ...
Videos
Objects simply contains a set of utility methods that are useful in combination with Object instances. Note that it can't be instantiated (it's final and it has no public constructor) and only contains static methods.
The naming schema of putting utility methods in pluralized-name classes is pretty common in the JDK:
CollectionsArrays(although strictly speaking there is no correspondingArrayclass)- ...
Other libraries also use this scheme, for example Guava:
MapsStrings- ...
One typical use of Objects class:
public void foo(SomeClass bar) {
Objects.requireNonNull(bar, "custom msg"); // // Ensure an object is not null.
}
Output when bar is null:
Exception in thread "main" java.lang.NullPointerException: custom msg
at java.util.Objects.requireNonNull(Unknown Source)
at com.example.ObjectsUsage.main(ObjectsUsage.java:24)
Another one to construct hashCode from the fields:
@Override public int hashCode() {
return Objects.hash(this.foo, this.bar, this.duh);
}
And the most useful one:
if (Objects.equals(sun, moon)) {
log("I swear I am in earth");
}