Wrapper Classes Flashcards

1
Q

Define a Wrapper class !

A

In Java, a wrapper class is a class that provides an object representation for primitive data types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens if we want to add primitive types to an ArrayList ?

A

When trying to add a primtive int to an ArrayList of Integers the java compiler automatically handles the conversion between primitive types and their corresponding wrapper classes during compilation. This means that when you assign a primitive to a wrapper class or vice versa, the compiler inserts the necessary code to perform the conversion, so you don’t have to do it manually

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give the Wrapper class of each primitive type and an example of constructing !

A

Boolean : new Boolean(true);
Byte : new Byte((byte)7);
Short : new Short((short)27)
Integer : new Integer(127)
Long : new Long(19)
Float : new Float(1.7)
Double : new Double(2.9)
Character : new Character(‘C’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give the signature of intValue method of Integer class !

A

public int intValue() :
Integer N = new Integer(20);
int a = N.intValue();
System.out.println(a); // Prints 20

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define autoboxing !

A

Autoboxing is converting a primtive type value to its corresponding Wrapper class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give a particularity of parse methods in JAVA !

A

Parse methods in Java, such as Integer.parseInt(String s) and Double.parseDouble(String s), are used to convert a String representing a numeric value into the corresponding primitive type (like int, double, etc.). These methods throw a NumberFormatException if the String is not a valid representation of the numeric type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give the method signatures of the methods parseInt and valueOf !

A

Integer.parseInt(String s): Converts a String to an int.

Double.parseDouble(String s): Converts a String to a double.

Float.parseFloat(String s): Converts a String to a float.

Long.parseLong(String s): Converts a String to a long.

Short.parseShort(String s): Converts a String to a short.

Byte.parseByte(String s): Converts a String to a byte.

public static Integer valueOf(String s) : Returns an Integer object holding the value of the specified String. Throws NumberFormatException if the String cannot be parsed.

public static Double valueOf(String s) : Returns a Double object holding the value of the specified String. Throws NumberFormatException if the String cannot be parsed

public static Float valueOf(String s) : Returns a Float object holding the value of the specified String. Throws NumberFormatException if the String cannot be parsed

public static Long valueOf(String s) : Returns a Long object holding the value of the specified String. Throws NumberFormatException if the String cannot be parsed

public static Byte valueOf(String s) : Returns a Byte object holding the value of the specified String. Throws NumberFormatException if the String cannot be parsed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Give a pitfall related to each one of those methods !

A

If the String passed in is not valid for the given type, Java throws
an exception. In these examples, letters and dots are not valid for an integer value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why the Character class doesn’t participate in the parse/
valueOf methods ?

A

The Character class doesn’t participate in the parse/valueOf methods.

Since a String is made up of characters, you can just call charAt() normally.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give for each Wrapper class the methods used for converting
String to primitive and String to wrapper class !

A

The parse methods implemented in Wrapper classes are used to convert a String parameter to a primitive type

The valueOf methods implemented in Wrapper classes are used to convert a String to a Wrapper class type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly