Wrapper Classes Flashcards
Define a Wrapper class !
In Java, a wrapper class is a class that provides an object representation for primitive data types.
What happens if we want to add primitive types to an ArrayList ?
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
Give the Wrapper class of each primitive type and an example of constructing !
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’)
Give the signature of intValue method of Integer class !
public int intValue() :
Integer N = new Integer(20);
int a = N.intValue();
System.out.println(a); // Prints 20
Define autoboxing !
Autoboxing is converting a primtive type value to its corresponding Wrapper class
Give a particularity of parse methods in JAVA !
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
Give the method signatures of the methods parseInt and valueOf !
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
Give a pitfall related to each one of those methods !
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
Why the Character class doesn’t participate in the parse/
valueOf methods ?
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.
Give for each Wrapper class the methods used for converting
String to primitive and String to wrapper class !
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