Wrapper Class in Java Interview Questions and Answers for 2 years experience

Java Wrapper Classes Interview Questions
  1. What is a wrapper class in Java?

    • Answer: Wrapper classes in Java are classes that provide a mechanism to convert primitive data types into objects. This allows primitives to be used in situations where objects are required, such as storing them in collections (like ArrayList or HashSet) or using them with methods that expect objects as parameters.
  2. Name the wrapper classes for each primitive data type.

    • Answer:
      • byte - Byte
      • short - Short
      • int - Integer
      • long - Long
      • float - Float
      • double - Double
      • boolean - Boolean
      • char - Character
  3. Explain autoboxing and unboxing.

    • Answer: Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class. Unboxing is the reverse process – converting a wrapper class object to its corresponding primitive type. This feature was introduced in Java 5 to simplify the code and make it more readable.
  4. Give an example of autoboxing.

    • Answer: Integer i = 10; Here, the primitive int value 10 is automatically boxed into an Integer object.
  5. Give an example of unboxing.

    • Answer: int x = i; Here, the Integer object i is automatically unboxed to its primitive int value.
  6. What is the purpose of the valueOf() method in wrapper classes?

    • Answer: The valueOf() method converts a primitive value or a String representation of a primitive value into a wrapper object. For example, Integer.valueOf("10") creates an Integer object with the value 10 from a String.
  7. What is the purpose of the parseInt() method in wrapper classes (e.g., Integer.parseInt())?

    • Answer: The parseInt() method parses a String and converts it into its corresponding primitive integer value. If the String cannot be parsed, it throws a NumberFormatException.
  8. How do wrapper classes handle null values?

    • Answer: Wrapper class objects can hold null values, unlike primitive types which cannot be null. This is crucial for handling situations where a value might be absent or not yet defined.
  9. What are the advantages of using wrapper classes?

    • Answer: Advantages include the ability to store primitives in collections, pass primitives as arguments to methods requiring objects, and the ability to represent the absence of a value using null.
  10. What are the disadvantages of using wrapper classes?

    • Answer: Disadvantages include slightly higher memory overhead compared to primitives and the potential for performance implications due to autoboxing and unboxing operations, although the JVM optimizes these in many cases.
  • Explain the difference between `Integer` and `int`?

    • Answer: `int` is a primitive data type while `Integer` is its wrapper class. `int` stores the value directly, while `Integer` stores the value as an object. `int` cannot be null, while `Integer` can be null. Autoboxing/Unboxing allows for automatic conversions between the two.
  • How does `Integer.MAX_VALUE` work?

    • Answer: `Integer.MAX_VALUE` is a static constant in the `Integer` class that holds the maximum value an `int` data type can represent (2,147,483,647).
  • What is the purpose of the `equals()` method in wrapper classes?

    • Answer: The `equals()` method compares the *values* of two wrapper objects, not their object references. This differs from the `==` operator which compares object references.
  • What exception might be thrown by `Integer.parseInt()`? How can you handle it?

    • Answer: A `NumberFormatException` is thrown if the String cannot be parsed into an integer. This can be handled using a `try-catch` block.
  • Are wrapper classes immutable?

    • Answer: Yes, wrapper classes are immutable. Once a wrapper object is created, its value cannot be changed.
  • How can you compare two `Integer` objects for equality?

    • Answer: Use the `equals()` method, not the `==` operator. `==` compares references, while `equals()` compares values.
  • Explain the use of `Integer.toString()`

    • Answer: This method converts an `Integer` object to its String representation.
  • What is the difference between `Integer.valueOf(10)` and `new Integer(10)`?

    • Answer: `Integer.valueOf(10)` is preferred because it utilizes a cache of frequently used `Integer` objects, improving performance. `new Integer(10)` always creates a new object.
  • Can you use wrapper classes in a switch statement? If so, how?

    • Answer: Yes, but only in Java 7 and above. You can use the values directly in the case statements

  • Thank you for reading our blog post on 'Wrapper Class in Java Interview Questions and Answers for 2 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!