Wrapper Class in Java Interview Questions and Answers for internship
-
What is a Wrapper class in Java?
- Answer: Wrapper classes in Java provide a mechanism to convert primitive data types (like `int`, `float`, `boolean`, etc.) into objects. This is crucial for situations where objects are required, such as adding primitives to collections (e.g., ArrayList, HashSet).
-
Name the wrapper classes for each primitive data type.
- Answer: `int` - `Integer`, `float` - `Float`, `double` - `Double`, `boolean` - `Boolean`, `char` - `Character`, `byte` - `Byte`, `short` - `Short`, `long` - `Long`
-
Explain autoboxing and unboxing.
- Answer: Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class object. Unboxing is the reverse process – converting a wrapper class object to its primitive type. This feature was introduced in Java 5 to simplify code.
-
Give an example of autoboxing.
- Answer: `Integer i = 10;` The integer literal `10` is automatically boxed into an `Integer` object.
-
Give an example of unboxing.
- Answer: `int j = i;` The `Integer` object `i` is automatically unboxed to its primitive `int` value.
-
What are the benefits of using wrapper classes?
- Answer: They allow primitives to be used in collections, they provide methods for working with primitives as objects (like `parseInt()`), and they facilitate null values (unlike primitives).
-
What is the purpose of the `valueOf()` method in wrapper classes?
- Answer: The `valueOf()` method converts a string representation of a primitive type into its corresponding wrapper object. For example, `Integer.valueOf("10")` converts the string "10" to an `Integer` object.
-
What is the purpose of the `parseInt()` method in `Integer` class?
- Answer: `parseInt()` converts a string representation of an integer into its primitive `int` equivalent. For example, `Integer.parseInt("10")` returns the primitive `int` value 10.
-
What is the difference between `==` and `.equals()` when comparing wrapper objects?
- Answer: `==` compares object references (memory addresses). `.equals()` compares the actual values of the objects. For wrapper objects, it's crucial to use `.equals()` for value comparison.
-
Explain the concept of immutability in wrapper classes.
- Answer: Wrapper class objects are immutable. Once created, their values cannot be changed. Any operation that seems to modify a wrapper object actually creates a new object with the modified value.
-
What are the methods available in the `Character` wrapper class? Discuss a few examples.
- Answer: The `Character` class offers methods like `isDigit()`, `isLetter()`, `isUpperCase()`, `toLowerCase()`, `toUpperCase()`, etc. These methods are used to check properties of characters and convert their case.
-
How would you handle a `NullPointerException` when dealing with wrapper objects?
- Answer: You can use conditional statements (e.g., `if (myInteger != null)`) or the Optional class from Java 8 to check for null before performing operations on wrapper objects to prevent `NullPointerException`.
Thank you for reading our blog post on 'Wrapper Class in Java Interview Questions and Answers for internship'.We hope you found it informative and useful.Stay tuned for more insightful content!