OCAJP – Wrapper Classes in Java

OCAJP – Wrapper Classes in Java

April 21, 2023 Off By Evelyn

This post will discuss the OCAJP exam objective, “Develop code using wrapper classes such a Boolean double, and integer”. The exam will test your knowledge about Autoboxing and Unboxing concepts for Wrapper classes.
Take our free OCAJP 8 test now! !

What are Wrapper Classes?
When working with numbers, we generally use primitive data types like byte, int and long.
In certain cases, objects can be used in place of primitives. The Java platform offers wrapper classes for each primitive data type.
These classes “wrap” the object’s primitive data type value. This is why they are called “Wrapper Classes”.
All wrapper classes can be modified.
When should you use wrapper classes?
You can use collections to store numeric data. Because collection objects cannot store primitive values.
Use constants defined by the class such as MIN_VALUE or MAX_VALUE that provide the upper and lowest bounds of the data types.
To convert String representation of primitive values into primitive data type.
Java has a wrapper class for every primitive data type.
As shown in the above image, Number is the super class for every numeric classes such as Byte, Short,Integer,Long,Float,Double.
Number contains four important abstract methods that are implemented by its sub-classes.
Below are the declarations for four abstract methods in Number Class (abstract class), which are used to convert primitive value.
Public abstract int long Value()public Abstract float floatValue()public Abstract double Value()
The exam covers the following classes: Integer, Double, and Boolean.
Integer
Here are some important facts about the Integer Class:
Since Java 1.0 version, integer class is in the java.lang package.
Number class is the superclass of Integer.
It implements Comparable,Serializable interfaces.
It is an immutable class.
The Integer type wraps a primitive value of the type int within an object. An object of type Integer has one field whose type it is int.
This class also provides methods to convert an int into a string and a string to an int.
Creating Integer Object
Two constructors are available for the integer class.
public Integer (int value)

It creates a new Integer objects that represent the specified int value.
public Integer(String s)throws NumberFormatException

It creates an Integer object representing 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 objects 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 4In 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 representations of primitive integer values into primitive integer values.
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 8toBinaryString, toOctalString, toHexString
public static String toHexString (int i).It returns a string representation for the integer argument as an unsigned integer base 16.
public static String toOctalString (int i). It returns a string representation for the integer argument as an unsigned integer base 8.
public static String toBinaryString (int i).It returns a string representation for the integer argument as an unsigned number 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 1000valueOf,byteValue, doubleValue, floatValue,intValue,longValue
public static Integer valueOf (int i).It returns an Integer instance corresponding to the specified int value.