Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When we override the toString() method we should always return a string representation of the object?

In practice, the toString method should return a String representation of the object.

In one project, I found some classes that override the toString method allowing return null.

Like:

@Override
public String toString() {
    .......
    return null;
}

For me, this practice is a violation to the principal propose of toString method that should return a String representation of the object.

The API documentation for Object.toString() says:

public String toString()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

When we override the toString() method of Object, you should never return null?

like image 677
Jesus Zavarce Avatar asked Sep 03 '15 14:09

Jesus Zavarce


People also ask

What happen if we override toString () method?

By overriding the toString( ) method, we are customizing the string representation of the object rather than just printing the default implementation. We can get our desired output depending on the implementation, and the object values can be returned.

Should toString return string?

The toString method is used to return a string representation of an object. If any object is printed, the toString() method is internally invoked by the java compiler. Else, the user implemented or overridden toString() method is called.

What does a toString () method returns as a string?

A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.


2 Answers

toString is mainly used for representing things where the audience is the programmer, like debugging and logging, where we need to see some text version of an object. Use information from the object that will be useful to you when you see it in a log file or in the debugger. From Effective Java, Chapter 3, item 10:

While it isn’t as important as obeying the equals and hashCode contracts (Item 8, Item 9), providing a good toString implementation makes your class much more pleasant to use. The toString method is automatically invoked when an object is passed to println, printf, the string concatenation operator, or assert, or printed by a debugger.

The advice in the API documentation for Object#toString is:

In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

Returning null would never count as informative, and could easily be misleading. It seems like someone was using toString to do something it was never intended for.

like image 166
Nathan Hughes Avatar answered Sep 16 '22 14:09

Nathan Hughes


The Java Language Specification does not prohibit returning null from toString(). Instead, it explicitly acknowledges that null may be returned.

Section 4.3.1: Objects

The string concatenation operator + (§15.18.1), which, when given a String operand and a reference, will convert the reference to a String by invoking the toString method of the referenced object (using "null" if either the reference or the result of toString is a null reference)

The text you cite is from the API documentation for Object.toString(). Like you, I read it to strongly imply that a non-null value is returned.

Returning a non-null value will be most useful. Returning null may be confusing in debuggers, making it difficult to distinguish between a null reference and a perfectly good non-null reference with an unfortunately null return value from toString().

like image 20
Andy Thomas Avatar answered Sep 16 '22 14:09

Andy Thomas