This post is about the OCAJP exam objective “Develop code that uses wrapper classes such as Boolean, Double, and Integer“. In the exam, you will be mainly tested on Autoboxing and Unboxing concepts of Wrapper classes, Using wrapper classes with their corresponding primitive types.
This article tests your knowledge on how to use the wrapper class that is mostly asked for the OCAJP certification exams. It is important for you to understand the concepts and practice with a lot of practice questions to master every concept.
What is Wrapper Classes?
Also Read:
Wrapper classes that used for wrapping the primitive types. The examples are Integer, Float, etc. These classes are introduced from Java 5 to simplify the use of primitive types as objects. Here are some of the points that are important to remember for the OCAJP certification exams:
- Generally, when we work with numbers we use primitive data types such as byte, int, long, double, etc.
- There are certain situations to use objects in place of primitives and the Java platform provides wrapper classes for each of the primitive data types.
- These classes “wrap” the primitive data type value in an object. Hence the name “Wrapper Classes”.
- All wrapper classes are immutable classes.
When to use Wrapper classes?
Here are some of the important points to remember when to use the wrapper classes.
- When you are working collections to store numeric values. Because collection objects can’t store primitive values.
- To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
- To convert String representation of primitive value into primitive data type.
- In Java, For every primitive data type, there is corresponding wrapper class.
- As shown in the above image, Number is the superclass for every numeric class such as Byte, Short, Integer, Long, Float, Double.
- The number contains four important abstract methods which are implemented by its subclasses.
- The below are the declarations of four abstract methods in Number class(abstract class) which are used to convert to a primitive value.
public abstract int intValue() public abstract long longValue() public abstract float floatValue() public abstract double doubleValue()
- You don’t need to learn all classes for the exam.
- Integer, Double, Boolean classes are on the exam.
In the following sections, I am going to write about most common wrapper classes and how to use them.
Integer
Here are some important points about the Integer class:
- Integer class is in java.lang package since Java 1.0 version.
- Number class is the superclass of Integer.
- It implements Comparable,Serializable interfaces.
- It is an immutable class.
- The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
- In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.
Creating Integer Object
Integer class has two constructors.
- public Integer(int value)
It constructs a new Integer object that represents the specified int value.
- public Integer(String s)throws NumberFormatException
It constructs a new Integer object that represents the int value indicated by the String parameter. The string is converted to an int value in exactly the manner used by the parseInt method for radix 10.
It throws NumberFormatException if the String does not contain a parsable integer.
- You can also create Integer object without a constructor.
Example :
public class IntegerDemo { public static void main(String[] args) { Integer n1 = new Integer("5"); Integer n2 = new Integer(2); Integer n3 = 4; // Integer n4 = new Integer("A"); this statement generates NumberFormatException. System.out.println(n1 + " " + n2 + " " + n3);//prints 5 2 4 } }
- In the above program, you are storing primitive int value into Integer objects, it is called Boxing.
Important Integer methods
public static int parseInt(String s) throws NumberFormatException
- This method converts the String representation of primitive integer value into a primitive integer value.
- It throws NumberFormatException if the String does not contain a parsable integer.
Example :
ublic class IntegerDemo { public static void main(String[] args) { int n1 = Integer.parseInt("8"); // int n2 = Integer.parseInt("OCA"); This generates NumberFormatException. System.out.println(n1);//prints 8 } }
- public static String toHexString(int i): It returns a string representation of the integer argument as an unsigned integer in base 16.
- public static String toOctalString(int i): It returns a string representation of the integer argument as an unsigned integer in base 8.
- public static String toBinaryString(int i); It returns a string representation of the integer argument as an unsigned integer in base 2.
Example :
public class IntegerDemo { public static void main(String[] args) { String s1 = Integer.toHexString(8); String s2 = Integer.toOctalString(8); String s3 = Integer.toBinaryString(8); System.out.println(s1 + " " + s2 + " " + s3); // prints 8 10 1000 } }
- public static Integer valueOf(int i): It returns an Integer instance representing the specified int value.
- public byte byteValue(): It returns the value of this Integer as a byte after a narrowing primitive conversion.
- public double doubleValue(): It returns the value of this Integer as a double after a widening primitive conversion.
- public float floatValue(): It returns the value of this Integer as a float after a widening primitive conversion.
- public int intValue(): It returns the value of this Integer as an int.
- public long longValue(): It returns the value of this Integer as a long after a widening primitive conversion.
Example :
public class IntegerDemo { public static void main(String[] args) { Integer in = Integer.valueOf(8); byte b = in.byteValue(); int i = in.intValue(); long l = in.longValue(); float f = in.floatValue(); double d = in.doubleValue(); System.out.println(in+" "+b+" "+i+" "+l+" "+f+" "+d);//It prints 8 8 8 8 8.0 8.0 } }
Autoboxing and Unboxing
Boxing conversions are that converting from primitive types to wrapper and wrapper classes to primitive types. You don’t have to explicitly do the conversions. The boxing conversions happen implicitly. This is part of the Java 5.0 features.
List<Integer> li = new ArrayList<Integer>(); li.add(8);// line 1 li.add(new Integer(5));// line 2 int n = li.get(1);// line 3
- At line1 you just typed 8 primitive value java will create Integer object and stores in the ArrayList object, this process is called Autoboxing.
- At line2 you are creating the object. No Autoboxing here.
- At line3 get method returns Integer object and that is converted into primitive int value when it is assigning to the int variable, this process is called Auto unboxing.
Double
Here are some important points about the Double class:
- The double class is in java.lang package since Java 1.0 version.
- Number class is the superclass of Double.
- It implements Comparable,Serializable interfaces.
- It is an immutable class.
- The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.
- In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double.
Creating Double Object
The double class has two constructors.
- public Double(double value)
It constructs a new Double object that represents the specified double value.
- public Double(String s)throws NumberFormatException
It constructs a new Double object that represents the double value indicated by the String parameter. The string is converted to a double value in exactly the manner used by the parseInt method for radix 10.
It throws NumberFormatException if the String does not contain a parsable number.
- You can also create a Double object without a constructor.
Example :
public class DoubleDemo { public static void main(String[] args) { Double n1 = new Double("5.0"); Double n2 = new Double(2.0); Double n3 = 4.0; // Double n4 = new Double("A"); this statement generates NumberFormatException. System.out.println(n1 + " " + n2 + " " + n3);//prints 5.0 2.0 4.0 } }
- In the above program, you are storing the primitive double value into Double objects, it is called Boxing.
Important Double methods
parseDouble
public static double parseDouble(String s) throws NumberFormatException
- This method converts the String representation of number value into primitive double value.
- It throws NumberFormatException if the String does not contain a parsable number.
Example :
public class DoubleDemo { public static void main(String[] args) { double n1 = Double.parseDouble("8.0"); double n2 = Double.parseDouble("2"); // double n3 = Double.parseDouble("OCA"); This generates NumberFormatException. System.out.println(n1+" "+n2);//prints 8.0 2.0 } }
- public static Double valueOf(double i): It returns a Double instance representing the specified double value.
- public byte byteValue(): It returns the value of this Double as a byte after a narrowing primitive conversion.
- public double doubleValue(): It returns the value of this Double as a double after a widening primitive conversion.
- public float floatValue(): It returns the value of this Double as a float after a widening primitive conversion.
- public int intValue(): It returns the value of this Double as an int.
- public long longValue(): It returns the value of this Double as a long after a widening primitive conversion.
Example :
public class DoubleDemo { public static void main(String[] args) { Double in = Double.valueOf(8); byte b = in.byteValue(); int i = in.intValue(); long l = in.longValue(); float f = in.floatValue(); double d = in.doubleValue(); System.out.println(in+" "+b+" "+i+" "+l+" "+f+" "+d);//It prints 8.0 8 8 8 8.0 8.0 } }
Autoboxing and Unboxing
List<Double> li = new ArrayList<>(); li.add(8.0);// line 1 li.add(new Double(5));// line 2 double n = li.get(1);// line 3
- At line1 you just typed 8 primitive value java will create Double object and stores in the ArrayList object, this process is called Autoboxing.
- At line2 you are creating the object. No Autoboxing here.
- At line3 get method returns Double object and that is converted into primitive double value when it is assigning to double variable, this process is called Auto unboxing.
Boolean
Here are some important points about the Boolean class:
- The Boolean class is in java.lang package since Java 1.0 version.
- An object class is the superclass of Boolean.
- It implements Comparable,Serializable interfaces.
- It is an immutable class.
- The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.
- In addition, this class provides several methods for converting a boolean to a String and a String to a boolean, as well as other constants and methods useful when dealing with a boolean.
Creating Boolean Object
A boolean class has two constructors.
- public Boolean(boolean value)
It creates a Boolean object representing the value argument.
- public Boolean(String s)
It creates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string “true”. Otherwise, allocate a Boolean object representing the value false.
- You can create Boolean using predefined fields in the Boolean class.
Example :
public class DoubleDemo { public static void main(String[] args) { Boolean b1 = new Boolean(true); Boolean b2 = new Boolean("true"); Boolean b3 = new Boolean("false"); Boolean b4 = new Boolean("oca"); Boolean b5 = new Boolean("null"); Boolean b6 =Boolean.TRUE; Boolean b7 = Boolean.FALSE; System.out.println(b1+" "+b2+" "+b3+" "+b4+" "+b5+" "+b6+" "+b7);//prints true true false false false true false } }
- public static boolean parseBoolean(String s): It Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
- public static Boolean valueOf(boolean b): It returns a Boolean instance representing the specified boolean value.
- public boolean booleanValue(): It returns the value of this Boolean object as a boolean primitive.
Example :
public class DoubleDemo { public static void main(String[] args) { boolean b1 = Boolean.parseBoolean("true"); boolean b2 = Boolean.parseBoolean("oca"); Boolean b3 = Boolean.valueOf(false); boolean b4 = b3.booleanValue(); System.out.println(b1 + " " + b2 + " " + b3 + " " + b4);// It prints true false false false } }
Conclusion
For OCAJP exam concentrate on Autoboxing and Auto unboxing, ParseXXX methods in three classes and possible ways to create the Boolean object. You have to remember the key concepts to answer all the questions related to the wrapper classes. Also, do a lot of practice questions to get more understanding of all the concepts.
Additional Resources