Wrapper Classes in Java

Introduction

Wrapper classes in Java provide object representations of primitive data types, enabling their use in collections, generics, and APIs requiring objects. This interview guide explains internal working, autoboxing, unboxing, memory behavior, and performance considerations relevant for developers with 3–5 years of experience.

What Interviewers Expect

  • Strong understanding of primitive vs wrapper differences.
  • Knowledge of autoboxing/unboxing and performance impact.
  • Internal details such as caching behavior and memory usage.
  • Ability to explain API use cases and common pitfalls.

Table of Contents

  • Interview Questions
  • Scenario-Based Interview Questions
  • Common Mistakes
  • FAQs

Q1. What are wrapper classes in Java?

  • Wrapper classes represent primitive data types as objects.
  • Examples include Integer, Double, Character, and Boolean.
  • They provide utility methods such as parsing and conversions.
  • Used in collections and generics since primitives are not allowed.

Follow-up Questions:

  • Why cannot collections store primitives?

Q2. List all wrapper classes and their corresponding primitives.

  • byte → Byte
  • short → Short
  • int → Integer
  • long → Long
  • float → Float
  • double → Double
  • char → Character
  • boolean → Boolean

Q3. What is the need for wrapper classes?

  • Enables use of primitive values in collections.
  • Supports parsing and converting string representations.
  • Provides immutability and safety for numeric operations.
  • Required by reflection and serialization APIs.

Q4. What is autoboxing?

  • Automatic conversion of primitive values to wrapper objects.
  • For example, int → Integer when stored in a List.
  • Performed by the compiler through valueOf() methods.
  • Uses cached objects when possible.

Follow-up Questions:

  • Explain Integer.valueOf() internal logic.

Q5. What is unboxing?

  • Automatic conversion of wrapper objects back to primitives.
  • Occurs in arithmetic operations or assignments requiring primitives.
  • Uses intValue(), doubleValue(), etc.
  • Can throw NullPointerException when object is null.

Q6. How does Integer caching work?

  • Integer values between -128 and 127 are cached.
  • Integer.valueOf() returns cached instances for this range.
  • Improves memory usage and performance.
  • Caching behavior is determined by JVM implementation.

Q7. Why does autoboxing increase memory usage?

  • Each autoboxed value creates an object if not cached.
  • Objects consume more memory than primitives.
  • Frequent autoboxing causes garbage collection overhead.
  • Leads to performance degradation in loops.

Q8. How does autoboxing affect performance?

  • Introduces additional object creation at runtime.
  • Causes frequent GC cycles when large numbers are processed.
  • May degrade performance in high-throughput systems.
  • Better to use primitives in performance-critical loops.

Q9. Are wrapper classes immutable?

  • Yes, all numeric wrapper classes are immutable.
  • Their internal value cannot be changed after creation.
  • Immutability ensures thread safety for shared instances.

Q10. What happens when comparing wrappers with == operator?

  • == checks reference equality, not value equality.
  • May return true for cached values in Integer.
  • Outside cache range, == returns false for equal values.
  • equals() should be used for value comparison.

Q11. How do wrapper classes handle null values?

  • Wrapper objects can be null, unlike primitives.
  • Unboxing a null reference throws NullPointerException.
  • Must validate wrapper inputs before arithmetic operations.

Q12. What is the difference between valueOf() and parseInt()?

  • valueOf() returns an Integer object.
  • parseInt() returns an int primitive.
  • valueOf() may return cached instances.
  • parseInt() is used for numeric parsing without object creation.

Q13. Can wrapper classes be used in switch statements?

  • Supported since Java 5 for Integer, Character, Short, and Byte.
  • Internally unboxed to primitives during switch evaluation.
  • Null values cause NullPointerException in switch.

Q14. How does JVM store wrapper objects in memory?

  • Wrapper objects are stored in heap memory.
  • Reference variables store pointers in stack memory.
  • Cached wrapper objects may be stored in internal pools.

Q15. What is the numeric promotion behavior with wrappers?

  • During arithmetic, wrappers are unboxed to primitives.
  • After computation, results may be autoboxed back.
  • May cause unnecessary boxing/unboxing cycles.

Q16. Why do wrappers support caching?

  • Small integer values occur frequently in applications.
  • Caching improves memory efficiency.
  • Reduces object creation and GC overhead.

Q17. Why are wrapper classes final?

  • Prevents subclassing for security reasons.
  • Ensures immutability and stable behavior.
  • Prevents modification of core numeric logic.

Q18. Can wrappers be compared using equals()?

  • equals() compares the internal value.
  • Works consistently across all numeric wrappers.
  • Should be preferred over == for object comparisons.

Q19. What is Number class in Java?

  • Abstract superclass for numeric wrappers.
  • Defines methods like intValue() and doubleValue().
  • Provides a common hierarchy for numeric conversions.

Q20. How do wrappers support polymorphism?

  • Numeric wrappers extend Number class.
  • Methods return different primitive types.
  • Supports polymorphic access in APIs expecting Number.

Scenario 1: Performance issue due to autoboxing

  • Large loops using Integer instead of int degrade performance.
  • Excessive boxing/unboxing increases GC pressure.
  • Fix by replacing wrappers with primitives in tight loops.

Scenario 2: NullPointerException in unboxing

  • User input stored in wrapper may be null.
  • Unboxing during arithmetic leads to NullPointerException.
  • Always check for null before unboxing.

Scenario 3: Incorrect comparison using ==

  • Using == for wrapper comparison may give unexpected results.
  • Cache range causes inconsistent reference equality.
  • Use equals() for reliable value comparison.

Common Mistakes

  • Using wrappers instead of primitives without need.
  • Incorrect comparison using == instead of equals().
  • Ignoring null values leading to unboxing exceptions.

Quick Revision Snapshot

  • Wrappers convert primitives into objects.
  • Autoboxing/unboxing done automatically by compiler.
  • Integer caching improves memory usage.
  • Unboxing null causes NullPointerException.
  • Primitives are faster and memory efficient.

FAQs

  • Q1: Can wrapper classes be modified?
    No, they are immutable.
  • Q2: Why collections require wrapper types?
    Collections cannot store primitives, only objects.
  • Q3: Are wrapper classes stored in heap?
    Yes, wrapper objects reside in heap memory.

Conclusion

Wrapper classes are essential for converting primitives into objects, enabling their use in collections, generics, and APIs requiring object types. Understanding autoboxing, unboxing, memory impact, and caching behavior is crucial for developers with 3–5 years of Java experience.

Scroll to Top