toString() Method in Java: Override and Examples - Quiz

Total: 5 questions

1. 

What is the toString() method in Java and what does it return by default?

toString() is a method of the Object class with the signature public String toString(). It returns a string representation of an object. Every object has it, because every class ultimately extends Object.

The default implementation is:

return getClass().getName() + "@" + Integer.toHexString(hashCode());

That is the fully qualified class name, an @ sign and the hash code in hexadecimal, for example oop.Person@5e2a3193. That's why you usually override toString() so the string includes the class name and the values of its main fields.

2. 

Is the number after @ in Person@5e2a3193 the object's memory address?

No. It is the value of hashCode() written in hexadecimal (Integer.toHexString(hashCode())). The JVM can move objects during garbage collection, while the hash code stays the same.

A less obvious consequence: if a class overrides hashCode() but not toString(), two equal objects print identically, for example oop.Person@1f and oop.Person@1f.

3. 

When is toString() called implicitly, without an explicit call in your code?

toString() is called implicitly by:

  • System.out.println(obj) and System.out.print(obj);
  • string concatenation: "Customer: " + person;
  • String.valueOf(obj), String.format("%s", obj), StringBuilder.append(obj);
  • loggers (SLF4J, Log4j) when an object is substituted into a message;
  • the IDE debugger when it shows a variable's value.

So writing println(person.toString()) is redundant, and if person is null, the explicit call throws NullPointerException, while println(person) just prints null.

4. 

How can you generate toString() automatically, and what does a record's toString() look like?

Ways to autogenerate it:

  • IntelliJ IDEA: Alt+Insert (Cmd+N on macOS) → toString(); the default template produces Person{fullName='...', age=56, retired=false};
  • Eclipse: Source → Generate toString()...;
  • Lombok: the @ToString annotation (or @Data); exclude fields with @ToString.Exclude.

Records (record, since Java 16) get toString() automatically together with equals() and hashCode(), but with square brackets:

public record Point(int x, int y) { }
System.out.println(new Point(1, 2)); // Point[x=1, y=2]
5. 

What are the most common mistakes when overriding toString()?

  • Infinite recursion. If Department prints its Employee list and each Employee prints its Department, the call ends with StackOverflowError. On one side of the relationship, print only the id or name.
  • Leaking sensitive data. toString() ends up in logs, so never include passwords, card numbers or tokens.
  • A typo in the name. tostring() or toString(String s) is a new method, not an override; without @Override the compiler stays silent.
  • Parsing toString() output in program logic. The format is meant for humans and can change; use equals() for comparison and JSON libraries for serialization.
  • Returning null. It compiles, but callers expect a string, and obj.toString().length() fails with NullPointerException.
Page 1 of 1