OCAJP – Wrapper Classes in Java

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 about Autoboxing and Unboxing concepts of Wrapper classes, Using wrapper classes with their corresponding primitive types.

OCPJP 6 Free Test

What is Wrapper Classes?

  • 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?

  1. When you are working collections to store numeric values. Because collection objects can’t store primitive values.
  2. 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.
  3. 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 super class for every numeric classes such as Byte, Short,Integer,Long,Float,Double.
  • Number contain four important abstract methods which are implemented by its sub classes.
  • The below are the declarations of four abstract methods in Number class(abstract class) which are used to convert to 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.

Integer

Here are the some important points about the Integer class:

  • Integer class is in java.lang package since Java 1.0 version.
  • Number class is the super class of Integer.
  • It implements Comparable,Serializable interfaces.
  • It is a 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 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

parseInt

public static int parseInt(String s) throws NumberFormatException

  • This method converts String representation of primitive integer value into 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

	}
}

toBinaryString, toOctalString, toHexString

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
	}

}

valueOf,byteValue, doubleValue, floatValue,intValue,longValue

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

		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 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 int variable,this process is called Auto unboxing.

Double

Here are the some important points about the Double class:

  • Double class is in java.lang package since Java 1.0 version.
  • Number class is the super class of Double.
  • It implements Comparable,Serializable interfaces.
  • It is a 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 an double to a String and a String to an double, as well as other constants and methods useful when dealing with an double.

Creating Double Object

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 an 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 Double object without 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 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 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

	}

}

valueOf,byteValue, doubleValue, floatValue,intValue,longValue

public static Double valueOf(double i)
It returns an 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<Double>();
		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 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 the some important points about the Boolean class:

        • Boolean class is in java.lang package since Java 1.0 version.
        • Object class is the super class of Boolean.
        • It implements Comparable,Serializable interfaces.
        • It is a 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 an boolean to a String and a String to an boolean, as well as other constants and methods useful when dealing with a boolean.

Creating Boolean Object

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
		
	}

}

parseBoolean,valueOf

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 OCA Java Programmer exam concentrate on Autoboxing and Auto unboxing, ParseXXX methods in three classes and possible ways to create Boolean object.

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

1 thought on “OCAJP – Wrapper Classes in Java”

  1. Hello Stephan,

    Thank you for the comments. We have updated with correct format for converting from Wrapper to Primitive value.

    Thank You,
    Krishna

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top