All Flashcards
Why use wrapper classes?
To use primitive types in contexts that require objects, such as collections or methods expecting objects.
Can Integer objects be null?
Yes, unlike the primitive int
, an Integer
object can be assigned a null value.
What is the purpose of the intValue()
method?
It returns the int
value of an Integer
object.
What is the purpose of the doubleValue()
method?
It returns the double
value of a Double
object.
Why are wrapper classes useful with collections?
Collections like ArrayList
can only store objects, not primitive types. Wrapper classes allow you to store primitive data in collections.
What is the difference between int
and Integer
?
int
is a primitive type; Integer
is a class that wraps the int
type. Integer
can be null, int
cannot.
What is the difference between double
and Double
?
double
is a primitive type; Double
is a class that wraps the double
type. Double
can be null, double
cannot.
What are the differences between Integer.parseInt()
and the Integer
constructor?
Integer.parseInt()
parses a String to an int. The Integer
constructor creates an Integer
object from an int or String.
What are the differences between Double.parseDouble()
and the Double
constructor?
Double.parseDouble()
parses a String to a double. The Double
constructor creates a Double
object from a double or String.
What does the following code output?
Integer num = new Integer(10); int val = num.intValue(); System.out.println(val);
10
What does the following code output?
Double num = new Double(3.14); double val = num.doubleValue(); System.out.println(val);
3.14
What does the following code output?
Integer num = 5; int val = num; System.out.println(val);
5
What does the following code output?
Double num = 2.718; double val = num; System.out.println(val);
2.718
What does the following code output?
Integer num = null; int val = 0; try { val = num; } catch (NullPointerException e) { System.out.println("NullPointerException caught!"); }
NullPointerException caught!