Course Content
Keywords
Operators
Loops
String
Array
Object Oriented Principle
Memory Management
Collection Framework
Exception Handling
Reflection API
Multi Threading
File Handling
Java Version wise Questions
Java Scenario Based Interview Questions
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 of Long where possible to reduce memory overhead.
  • Use Long.valueOf() instead of new 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