It seems that with Java 7, Objects class is providing a lot of functionality already covered in other parts of the language.
Take toString() for example. The following will produce the same results:
Objects.toString(12);
String.valueOf(12);
In fact, Objects.toString is defined as:
public static String toString(Object o) {
return String.valueOf(o);
}
Say we're dealing with actual classes. Is one preferred over another?
Objects.toString(o);
o.toString();
What are language designers telling us here? Should we start preferring Objects's solution instead of what's already available? What is a long term rationale for something like this?
See the documentation for said class:
This class consists of
staticutility methods for operating on objects. These utilities includenull-safe ornull-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.
So it's mostly to save you from an additional null guard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With