Wrapper Class in Java Interview Questions and Answers for freshers

100 Interview Questions on Wrapper Classes in Java
  1. What are Wrapper classes in Java?

    • Answer: Wrapper classes in Java provide a way to convert primitive data types (like `int`, `float`, `boolean`, etc.) into objects. Each primitive type has a corresponding wrapper class (e.g., `Integer` for `int`, `Float` for `float`, `Boolean` for `boolean`). This allows primitives to be used in situations where objects are required, such as collections (like `ArrayList`).
  2. Why do we need Wrapper classes?

    • Answer: We need wrapper classes to treat primitive data types as objects. This is essential for using primitives in collections, passing them as arguments to methods expecting objects, and leveraging object-oriented features like polymorphism and inheritance with primitive types.
  3. List the wrapper classes for each primitive type.

    • Answer: `Integer` (for `int`), `Long` (for `long`), `Short` (for `short`), `Byte` (for `byte`), `Float` (for `float`), `Double` (for `double`), `Character` (for `char`), `Boolean` (for `boolean`).
  4. 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 is handled automatically by the Java compiler from Java 5 onwards, making code cleaner.
  5. Give an example of autoboxing.

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

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

    • Answer: The `valueOf()` method converts a primitive type or a String representation into a wrapper class object. For example, `Integer.valueOf("10")` converts the String "10" into an `Integer` object.
  8. What is the purpose of the `parseInt()` method in wrapper classes (like `Integer`)?

    • Answer: `parseInt()` converts a String representation of a number into its corresponding primitive integer value. For example, `Integer.parseInt("10")` returns the `int` value 10.
  9. What is the difference between `Integer.parseInt()` and `Integer.valueOf()`?

    • Answer: `parseInt()` returns a primitive `int`, while `valueOf()` returns an `Integer` object. `valueOf()` is useful when you need an object, while `parseInt()` is useful when you need the primitive value directly.
  10. Explain the `intValue()` method in the `Integer` class.

    • Answer: The `intValue()` method returns the primitive `int` value represented by the `Integer` object. It's used for unboxing explicitly.
  11. Can you explain the concept of immutability in wrapper classes?

    • Answer: Wrapper class objects are immutable. Once an object is created, its value cannot be changed. Any operation that seems to modify the value actually creates a new object with the modified value.
  12. What happens when you compare two `Integer` objects using `==`?

    • Answer: When comparing `Integer` objects using `==`, you're comparing object references, not the values. Two `Integer` objects with the same value might not be equal using `==` unless they refer to the same object in memory. Use `.equals()` for value comparison.
  13. How do you compare two `Integer` objects for equality of value?

    • Answer: Use the `.equals()` method. For example: `integer1.equals(integer2)`.
  14. What is the `Integer` cache?

    • Answer: The `Integer` cache is an internal mechanism in the JVM that caches `Integer` objects for values between -128 and 127 (inclusive). If you create an `Integer` object with a value within this range, the JVM might return a reference to the cached object instead of creating a new one. This improves performance.
  15. Explain the behavior of `==` when comparing `Integer` objects with values within the cache range.

    • Answer: If two `Integer` objects are created with values within the cache range (-128 to 127), `==` might return `true` because both references could point to the same cached object.
  16. Explain the behavior of `==` when comparing `Integer` objects with values outside the cache range.

    • Answer: If the values are outside the cache range, `==` will always return `false` unless both variables explicitly refer to the same object.
  17. What are some common exceptions related to wrapper classes?

    • Answer: `NumberFormatException` is thrown when attempting to parse a String that doesn't represent a valid number using methods like `parseInt()` or `valueOf()`. `NullPointerException` can be thrown if you try to access methods on a `null` wrapper object.

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