Java Wrapper Classes Interview Questions
This section covers frequently asked Java wrapper class interview questions.
1. Purpose of Java Wrapper Classes.
Java wrapper classes provide object representations for primitive data types (int
, float
, boolean
, etc.).
2. Eight Primitive Data Types in Java.
(Lists the eight primitive data types: byte
, short
, int
, long
, float
, double
, char
, boolean
.)
3. Why Use Wrapper Classes?
Wrapper classes allow using primitives as objects, enabling features like null values, use in collections, and access to additional methods.
4. Autoboxing.
Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class object.
Example
int x = 10;
Integer y = x; // Autoboxing
5. Unboxing.
Unboxing is the automatic conversion of a wrapper object to its corresponding primitive type.
Example
Integer x = 10;
int y = x; // Unboxing
6. valueOf()
Method.
Creates a wrapper object from a string representation (e.g., Integer.valueOf("10")
).
7. ==
vs. equals()
for Wrapper Objects.
==
compares references; equals()
compares values.
8. hashCode()
Method.
Returns a hash code, important for hash-based collections (like HashMap
).
9. Converting Wrapper Objects to Primitives.
Use xxxValue()
methods (e.g., intValue()
, doubleValue()
).
10. parseInt()
Method.
Converts a String to an int
primitive.
11. compareTo()
Method.
Compares two wrapper objects based on their values.
12. Validating Primitive Type Strings.
Use valueOf()
in a try-catch
block to handle potential NumberFormatException
.
13. The Boolean
Class.
Wraps a boolean value, providing object-oriented representation and methods for boolean manipulation.
14. hashCode()
and equals()
in Boolean
.
Standard methods for comparing and hashing Boolean
objects.
15. Converting boolean
to Boolean
.
Use Boolean.valueOf()
.
16. toString()
Method.
Returns a String representation of the wrapper object's value.
17. valueOf()
in Character
.
Creates a Character
object from a char
value.
18. Wrapper Classes in Generics.
Wrapper classes can be used as type parameters in generic classes and methods.
19. doubleValue()
in Number
.
Converts the number's value to a double
.
20. Converting Wrapper Objects to Strings.
Use toString()
or string concatenation.
21. toBinaryString()
.
Converts an integer to its binary representation.
22. MAX_VALUE
and MIN_VALUE
Constants.
Represent the maximum and minimum values for a given primitive type.
23. parseXxx()
Methods.
Convert strings to primitives (e.g., Integer.parseInt()
).
24. Creating BigInteger
from a String.
Use the BigInteger
constructor.
25. intValueExact()
.
Returns the exact integer value; throws ArithmeticException
if the value is not an exact integer.
26. valueOf()
in BigDecimal
.
Creates a BigDecimal
object from a double.
27. setScale()
Method.
Sets the scale (number of decimal places) of a BigDecimal
.
28. Wrapper Classes in switch
Statements.
Since Java 7, you can use String and wrapper class objects in switch statements.
29. compare()
in Comparator
.
Compares two objects.
30. Converting LocalDate
to Date
.
(This section explains how to convert between `LocalDate` and `Date` objects, likely using methods from the `java.time` and `java.util` packages.)
31. intValue()
in Double
.
Converts a Double
object to an int
.
32. Comparing Double
Objects.
Use compareTo()
or compare with a small tolerance value (epsilon) to account for precision issues.
33. shortValue()
in Short
.
Converts a Short
object to a short
primitive.
34. floatValue()
in Float
.
Converts a Float
object to a float
primitive.
35. Why Wrap Primitive Types?
Wrapper classes enable using primitives as objects, allowing for null values and use in collections.
36. ==
vs. equals()
(Repeated from earlier).
==
compares references; equals()
compares values.
37. Significance of hashCode()
(Repeated from earlier).
Crucial for hash-based collections.