About Lesson
Scenario: Your application is running out of memory due to excessive use of Long
objects. How can you optimize memory usage?
Expected Answer:
- Use primitive
long
instead ofLong
where possible to reduce memory overhead. - Use
Long.valueOf()
instead ofnew Long()
to leverage the Long cache for values between -128 and 127.
Example:
long primitiveLong = 100L; // Memory efficient
Long cachedLong = Long.valueOf(100L); // Uses cached values