The class java.util.Objects
contains the deepEquals(Object a, Object b)
method that can be used to compare objects of any type (including arrays and null references), but doesn't contain a similar deepToString(Object o)
. This is disappointing. (By the way, the private constructor of this class contains the message "No java.util.Objects instances for you!" that explains to some extent why this class is so mean). That being the case, I've tried to implement the method myself:
public static String deepToString(Object o) {
if (o == null || !o.getClass().isArray())
return Objects.toString(o);
else
return Arrays.deepToString((Object[])o);
}
The problem is that it doesn't work with one-dimensional arrays of primitive types. Do I have to go through all primitive array types with nested else if
s and call corresponding Arrays.toString(...)
methods for them, or there is a simpler alternative?