From my experience I prefer to add a toString()
method in all POJO classes because it helps if you want to print the current state of an instance, using a logging framework or not.
The default toString()
method outputs a string formed from:
getClass().getName() + '@' + Integer.toHexString(hashCode())
.
That is not useful 99.8% of the time. Usually you want to read the value of your class attributes. For that you need to override toString()
.
You never know when you want to see its state outside of a debugging framework.
If you/someone else logs your instance to see what it contains, if you don't override the toString()
the output will only be the name and hash; that is not very informative.
The performance aspect is not influenced at all, neither is serialization (using a serious framework) as far as I know of.
I recommend using the source generation feature of your IDE to generate toString()
methods. It is fast and gives you more control.