when to use toString() method

In most languages, toString or the equivalent method just guarantees that an object can be represented textually.

This is especially useful for logging, debugging, or any other circumstance where you need to be able to render any and every object you encounter as a string.

Objects often implement custom toString behavior so that the method actually tells you something about the object instance. For example, a Person class might override it to return “Last name, First name” while a Date class will show the date formatted according to some default setting (such as the current user interface culture).

Leave a Comment