The Object class

  • Explain how every object in Java has a toString method.

Every class in Java is descended from the Object class. This is an implicit inheritance (i.e., it happens without appending extends Object to your class declarations). And since the Object class contains a toString() method, we can call toString() on any programmer-defined type and get its string representation.

Student john = new Student("John Doe", "john@e.mail");

System.out.println(john.toString()); // toString() inherited from the Object class
//-> it will print something like Student@1ee0005

System.out.println(john); // automatically calls john.toString()
//-> Student@1ee0005

By default, the toString method returns a string consisting of the name of the class and the (unsigned) hexadecimal representation of an object's hash code.

Aside: here is the default implementation of toString():

getClass().getName() + '@' + Integer.toHexString(hashCode())

The hashCode() returns an integer representation of the object. That integer, by default, is derived from the memory address of the object.

The default behavior of toString is often not useful. You can override it to return a different string representation of your object. It is common to make a string representation from the (value of) fields. For instance, for students, we can use their name followed by their email:

Student john = new Student("John Doe", "john@email.com");
System.out.println(john); 
//-> John Doe (john@email.com)

Here is the code to produce the desired effect:

public class Student {
   // fields and methods not shown for brevity

   @Override
   public String toString() {
      return name + " (" + email + ")";
   }
}

IntelliJ can automatically generate toString methods for you. Refer to JetBrain's Help documentation.

The Object class has several other methods, including equals and hashCode, which we will learn to override in future chapters.

Resources