toString()

Edit on Github instead

Most objects in Java have a toString() method, which returns a String-ified version of the object. This is one of the methods that you will most frequently override, since only you know how to display the objects you construct.

Some things in Java are self evaluating, which means they'll return themselves. You may've realized this initially, when we were evaluating primative data types - ints, doubles, and booleans:

System.out.println(true);           // Prints 'true'
System.out.println(42);             // Prints 42
System.out.println(87.18);          // Prints 87.18

Strings are also self-evaluating:

String greeting = "Hello";
System.out.println(greeting);           // Prints "Hello"
String goodbye = new String("Goodbye");
System.out.println(goodbye);            // Prints "Goodbye"

Arrays and other "container" objects are self-evaluating if they contain self-evaluating objects. For example, an array of booleans is self evaluating, while an array of Cars is not.

Suggest changes

Related links

URL Topic Source