Objects.toString [Re: What methods should go into a java.util.Objects class in JDK 7?] (original) (raw)
David Holmes - Sun Microsystems David.Holmes at Sun.COM
Thu Oct 8 23:53:36 UTC 2009
- Previous message: Objects.toString [Re: What methods should go into a java.util.Objects class in JDK 7?]
- Next message: Objects.toString [Re: What methods should go into a java.util.Objects class in JDK 7?]
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Joe,
Joseph D. Darcy said the following on 10/09/09 04:30:
System.out.println("" + referenceOfAnyType);
will print "null" if referenceOfAnyType is null. This is what the platform has done since the beginning.
Yes because String concatenation can not tolerate null values appearing, so it is defined to replace null with "null" (or replace a null from o.toString() with "null"). And I suspect that "null" was chosen over "" because it is then obvious when an unexpected null was encountered.
But Objects.toString(o) is not concerned with string concatenation so doesn't have to mimic that behaviour.
Personally I'd only put in the two-arg variant because:
a) a one-arg that simply forwards to String.valueOf is redundant as already discussed
b) a one-arg that hardwires the response to null (whether that be to return null or "null") will simply force the programmer to either switch to a different API or else add their own null handling logic - which defeats the purpose of putting in these utility/convenience methods.
So to me: String toString(Object o, String useIfNull) is the only method that provides true utility in this case.
David
- Previous message: Objects.toString [Re: What methods should go into a java.util.Objects class in JDK 7?]
- Next message: Objects.toString [Re: What methods should go into a java.util.Objects class in JDK 7?]
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]