Objects.toString [Re: What methods should go into a java.util.Objects class in JDK 7?] (original) (raw)
Eamonn McManus Eamonn.McManus at Sun.COM
Fri Oct 9 09:15:48 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 ]
David Holmes - Sun Microsystems wrote:
So to me: String toString(Object o, String useIfNull) is the only method that provides true utility in this case.
I agree with that, and would just suggest to the person specifying the method to add a @see String#valueOf(Object). I find that the behaviour of that method is usually exactly what I want when the argument is null (and I find the idea of representing null by an empty string aberrant, but there you go), so for people like me but who might not have noticed String.valueOf this will save them from peppering Objects.toString(x, "null") throughout their programs.
Éamonn McManus · JMX Spec Lead · http://weblogs.java.net/blog/emcmanus
David Holmes - Sun Microsystems wrote:
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 ]