Jesus Zavarce
Jesus Zavarce

Reputation: 1759

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?

Upvotes: 13

Views: 5441

Answers (6)

Nathan Hughes
Nathan Hughes

Reputation: 96394

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.

Upvotes: 11

user784540
user784540

Reputation:

From my point of view your should not return null in toString() methods.

It is not informative nor usable. In contrast, using toString() method to return correct and informative strings for your data model class instances will help you and your teammates to use your classes and incorporate them to more complex data structures.

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86419

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().

Upvotes: 8

code code
code code

Reputation: 35

This looks like customized usage of ToString(). This override method is purely based upon the implementation which user tries to achieve as per his/her needs. As per me returning null is no harm as long as user expects this value to be returned, as null could be stored in string data type.

But yes overriding ToString is not a good practice unless you got proper justification to do this.

Upvotes: -1

user4979686
user4979686

Reputation:

You should override the toString() method whenever it is practical for the application to do so.

As for returning null, I find that to be utterly useless as toString() is mostly used during debugging. So no, you should never return null from a toString() method.

You cannot fix stupid, but you can learn from it.

Upvotes: 1

Jacek Cz
Jacek Cz

Reputation: 1906

toString() is "human" presentation of Object, used like Nathan for debugging and as "speed method" of presentation in applications.

Sometimes poeple are mixing toString() with serialization / deserialization (must include all important (non-transient) fields) and be precise because of algorithm for reading values.

Upvotes: 0

Related Questions