2. Object.toString()
❖
Returns string informative representation of an object.
❖
Implementation in class Object returns
<class name>@<hashCode>
❖
Is overridden to provide better object view in logs
❖
Application business logic should not relay on a value
returned by this method
4. There is no problem unless
you're getting a High Load!
5. ❖
Depending on logging strategies, may be called very
frequently
❖
This may slow down your system significantly
❖
Creating new object instances (ToStringBuilder) with
method is cheap but not free.
7. Long Living Objects
❖
There are a limited number of long living objects
❖
A set of objects may change at runtime
❖
Used as Map keys
❖
toString is called frequently (logging)
❖
toString is called on the same instance multiple times
8. Long Living Objects. Solution
❖
Since the instance set is not known at compile time, it is
not possible to use Enums.
❖
Use object pool. You'll also be able to use identity
equals(==) and identity hashCode.
❖
Evaluate and cache toString value in field of the object
❖
It’s OK to use ToStringBuilder for evaluation
❖
Keep toString value as short as possible
14. ❖
Don't spend much time on optimization on early phases
of the project
❖
But don't put a performance bomb in your code. Try to
predict future object usage and don't write a code which
will definitely slow your application.