[Effective Java] [Item 6] Avoid creating unnecessary objects

Опубликовано: 01 Март 2026
на канале: Omkar Shetkar
944
15

https://github.com/Omkar-Shetkar/effe...

Creating and Destroying Objects

Item 6: Avoid creating unnecessary objects

Immutable objects are always reusable.
Prefer reuse of an object rather creating new functionally equivalent object
Don't create String with new operator
Prefer using static factory methods over constructors for creating instances
of immutable classes
Its better to cache expensive objects
Prefer primitives to boxed primitives, and watch out for unintentional
autoboxing
This item should NOT be misunderstood that "object creation is expensive and
should be avoided"
Its okay to create short lived and lightweight objects, since modern
garbage collectors are efficient
Avoiding creating unnecessary objects is only a matter of style and
performance concern
Many times creating new object is more secure than reusing an object
Moral of the story: Take a conscious decision while creating an object
Can I reuse it or create a new one ?