What methods should go into a java.util.Objects class in JDK 7?

David Holmes - Sun Microsystems David.Holmes at Sun.COM
Sun Nov 15 04:46:10 UTC 2009


Paul,

Paul Benedict said the following on 11/15/09 11:28:
> I would like to propose adding this method:
> 
> /**
>  * Selects the object if not {@code null}; otherwise fallsback to the
>  * specified default object.
>  *
>  * @param object the object to test
>  * @param defaultObject the default object
>  * @return the object if not {@code null}; otherwise the default object
>  */
> public static Object defaultIfNull(Object object, Object defaultObject) {
>     return (object != null) ? object : defaultObject;
> }

I don't get it. Why would anyone need the above when they can write:

     obj != null ? obj : otherObj

themselves ??? Compared to

     defaultIfNull(obj, otherObj)

you don't even save any keystrokes (and this is with a static import)!

David Holmes




More information about the core-libs-dev mailing list