The Object class has a method called, toString(). This method returns a string representation of the object of a normal class. All classes inherit this method from Class Object. Each array (as an object) has a similar method.
Unfortunately, this string representation of the object is a short text code (short string literal text). It is not very useful, though it can be decoded. Such decoding is not addressed in this article. And so, the programmer has to override this method in order to have a representation of the object; the computer user will appreciate that. The overriding is addressed in this article.
Default Behavior of toString() Method
Primitive Types
Primitive types such as the int exist in their own right. However, each primitive type in Java has a corresponding class (wrapper). When it comes to converting primitive objects to strings, it is the corresponding classes that should be used. The following program illustrates this for the int. The corresponding class for int is the Integer class.
The output is 5. If “Integer” was typed as int, then an error message would have been issued at compile time. The toString() method of the greatest ancestor class has been employed here, without any issue. That is, the integer, 5 has been converted to a string and printed without any issue. However, if the class was a programmer-defined class or another type of predefined class, then there would have been some issue.
Programmer Defined Class
Consider the following program that prints out the representation of the programmer defined object, obj:
The output is:
This is a short coded text – not very useful to the user. The user might have preferred something like:
prop2 => 2;
These are the different properties (fields) and their values. What separates a property from its value in the printout is “ => ”, which should be introduced by the programmer. In a question like this, the methods are not usually printed.
Array
Consider the following program, where the array as an object, arr, should be printed:
The output is,
which is another text code. Is that what you wanted? You would have loved to see something like:
where the element separator is “, ”.
List
Consider the following program, where an ArrayList as an object, al, should be printed:
The output is:
The output is quite good! This means that the programmer does not have to override the Object.toString() method when it concerns the ArrayList (or possible list in general). However, when it comes to programmer-defined objects or the array, the programmer needs to override the method.
Map
Consider the following program, where a HashMap as an object, hm, should be printed:
The output is:
The output is quite good! The key/value pairs are distinguishable, with the element separator, being, “, ”. This means that the programmer does not have to override the Object.toString() method, when it concerns the HashMap (or possible map in general). However, when it comes to programmer-defined objects or the array, the programmer needs to override the method.
The rest of this article deals with the overriding of the Object.toString() inherited method of the programmer-defined object and the array.
Overriding toString()
Array
With the array, today, the overriding is indirect or workaround. Java has a class called, Arrays. This class has the toString Method, already overridden by Java. In the class, the toString() method is static: this means that the Arrays class does not have to be instantiated for its toString() method to be used. Here, the toString() method takes an argument, which is the identifier of the array. It produces an output, where the separator is “, ”. Class Arrays, is in the java.util.* package. The following program shows the workaround for arrays:
The output is:
The output is quite good! And so, today, the programmer no longer needs to code an overriding method, for the toString() method, for Java array. The programmer does a workaround with Arrays and its toString().
Programmer Defined Object
With programmer defined class, the toString() method of the Object class, just has to be redefined, as illustrated in the following programmer defined class:
The technique is to use the string concatenation operator, + to be joining non-string literals with string literals. The preceding “@Override” prevents certain errors in the overridden method. Redefinition here is overriding. A programmer-defined class corresponds to the programmer-defined object. The following Java main() method is appropriate for the above class:
The output is:
prop2 => 2
This output is better appreciated by the user than the short text code, “AClass@6ff3c5b5”. Do not forget that the overriding definition takes place in the class of interest.
Conclusion
The Object class has a method called, toString(). This method returns a string representation of the object of a class. All classes inherit this method from Class Object. Each array (as an object) has a similar method. Each class needs overriding of this method, indirectly or directly.
With primitive types, use the reference types (e.g., Integer for int), where Java already has a predefined overridden toString() method, which is satisfactory. With lists and maps, too, Java already has a predefined overridden toString() method, which is satisfactory. With an array, do a workaround: use the toString() method of the Arrays class. With the programmer-defined class, do the actual overriding, using the string concatenation operator, +, as often as possible.