The useful method String.valueOf(Object) has been around since JDK 1.0 and is one of the JDK-provided methods that I use on a regular basis. The Objects class was introduced with JDK 1.7 and included the Objects.toString(Object) method. This post compares these two similar methods. Let's get to it!
Both methods String.valueOf(Object) and Objects.toString(Object) essentially do the same thing: call the toString() method on a passed-in object. This is the case as long as it's not null or it doesn't return the string "null" when null is passed to them. In short, both methods are designed to provide a simple approach for invoking an object's toString() without worry about a NullPointerException if it turned out to be null.
from DZone.com Feed https://ift.tt/2okXA1n
No comments:
Post a Comment