Java Wrapper Classes

 

Java language provides a dedicated wrapper class for each one of Java primitive types. Each one of those classes is a wrapper around the primitive type that stores the primitive’s value and provides a way for the primitive to have state and behavior. The wrapper classes can be constructed using a variable of the corresponding primitive type or String. Java language provides the following wrapper classes.

Wrapper Class Primitive Type Wrapper Class Constructors
Integer int Integer(int), Integer(String)
Boolean boolean Boolean(boolean), Boolean(String)
Long long Long(long), Long(String)
Short short Sort(short), Short(string)
Character char Character(char)
Double double Double(double), Double(String)
Float float Float(float), Float(double), Float(String)
Byte byte Byte(byte), Byte(String)

Number wrappers

java.lang.Number is the parent class of the wrapper classes for the numeric primitive types, which are int, short, byte, double, float, long.

How to Create a Number Object

Java defines 2 ways to create a number object.

1 – A wrapper object is initialized by using the object’s constructor.

Integer i = new Integer(“1”);

2 – Java provides the valueOf method for all the wrapper classes to create a wrapper object. The signature of the value of method is as follows:

public static <Wrapper Class> valueOf(String)

For example, an integer object can be created from a String using the valueOf() method as follows:

Integer i = Integer.valueOf(“1”);

Also, for the wrapper classes that are subclasses of the Number class, a primitive variable can be created from a String using the parseXXX method of the wrapper class. Every wrapper class defines its own parser method.

Integer public static int parseInt(String)

Double public static double parseDouble(String)

Float public static float parseFloat(String)

Byte public static byte parseByte(String)

Long public static long parseLong(String)

Short public static short parseShort(String)

For example, using the following statement, we can create a primitive integer variable and assign it the value 2.

int i = Integer.parseInt(“2”);

The primitive value of a Number class can be obtained using the xxxValue method. The signature of the method is as follows:

Integer public int intValue()

Double public double doubleValue()

Float public float floatValue()

Byte public byte byteValue()

Long public long longValue()

Short public short shortValue()

As an example, let’s create an Integer object using the String “1” and then get its value and assign it to a primitive.

Integer object = new Integer(“1”);

int i = object.intValue();

The value of the primitive int variable i would be 1.

What is NumberFormatException?

When creating a number object or parsing a primitive numeric type from a string, if the string object itself does not represent a number that can be assigned to the primitive variable properly, the method throws a NumberFormatException. For example, the following statement parses “Hello” string to assign it to an int primitive. However, “Hello” string does not represent an integer and the statement would throw a NumberFormatException.

int i = Integer.parseInt(“Hello”);

Similarly,the following statement would throw a NumberFormatException since the string that is parsed represents a double, not an integer.

int i = Integer.parseInt(“2.5”);

NumberFormatException is an unchecked exception. Therefore, it does not have to be caught or declared when a method that creates a number object or parses a primitive from a String is invoked.

Now, let’s see number classes in an example program.

public static MyClass {

public static void main(String [] args) {

Integer i = new Integer(“1”);

System.out.println(“i: “ + i.intValue());

Integer j = new Integer(2);

System.out.println(“j: “ + j.intValue());

Integer k = Integer.valueOf(“3”);

System.out.println(“k: “ + k.intValue());

int l = Integer.parseInt(“4”);

System.out.println(“l: “ + l);

}

}

The output would be

i: 1

j: 2

k: 3

l: 4

In this program, first a new Integer object is created from string “1” using Integer’s constructor and then the value of i is printed on the console. Similarly, Integer object j is created from primitive int 2 and then its value is printed on the console. Integer object k is created using the static valueOf() method and then its value is printed on the console. Following this, primitive integer l is created by parsing the string “4” and its value is printed on the console, too.

java.lang.Boolean class

java.lang.Boolean class is the wrapper for the boolean primitive. A Boolean wrapper object can be created using its constructor, from a String or a primitive boolean. Java also provides a static valueOf() method to create a Boolean object, with the following signature.

public static Boolean valueOf(String)

In addition to these, a boolean primitive can be created by parsing from a String, using the static parseBoolean() method. The signature of the parseBoolean method is as follows:

public static boolean parseBoolean(String)

The value of a boolean object can be obtained using the booleanValue() method. The signature of the method is as follows:

boolean booleanValue()

Let’s see Boolean objects in an example.

public class MyClass {

public static void main(String [] args) {

Boolean b1 = new Boolean(“true”);

Boolean b2 = new Boolean(false);

Boolean b3 = Boolean.valueOf(“true”);

boolean b4= Boolean.parseBoolean(“false”);

System.out.println(“b1: “ + b1.booleanValue() + “, b2: “ + b2.booleanValue() + “, b3: “ + b3 + “, “ + b4);

}

}

The output would be

b1:true, b2: false, b3: true, b4: false

java.lang.Character class

java.lang.Character is the wrapper class for the char primitive. A Character wrapper object can be created using its constructor, from a String or primitive char. In addition, Character class provides the static valueOf method to create a Character object from a String. The signature of the valueOf method is as follows:

public static Character valueOf(String)

Let’s see Character class in an example.

public class MyClass {

public static void main(String [] args) {

Character c1 = new Character(‘a’);

Character c2 = new Character(‘b’);

Character c3 = Character.valueOf(‘c’);

System.out.println(“c1: “ + c1.charValue() + “, c2: “ + c2.charValue() + “, c3: “ + c3);

}

}

The output would be

c1: a, c2: b, c3: c

What is Autoboxing?

Java automatically converts a primitive to its corresponding wrapper object if the primitive is used in place of a wrapper in a Java statement. This feature is known as Autoboxing. In its simplest form, using Autoboxing, wrapper objects can be created directly from their primitive types, without going through a constructor or any other wrapper method. For example, an Integer object can be created from a primitive int as follows:

Integer i = 2;

This is equivalent to

Integer i = new Integer(“2”);

Let’s see autoboxing in an example.

Public class MyClass {

public static void main(String [] args) {

Character c = ‘1’;

System.out.println(c.isDigit());

}

}

The output would be

true

Here, a Character object is initialized directly from a char, ‘1’, using autoboxing. Following this, isDigit() method is invoked on the Character object which returns true.

What is unboxing?

If a wrapper object is used as a primitive type, Java automatically converts the wrapper object to its corresponding primitive type. This feature is known as unboxing.

Let’s see unboxing with an example.

public class MyClass {

public static void main(String [] args) {

Integer i = new Integer(“1”);

Integer j = Integer.valueOf(“2”);

System.out.println(“i + j: “ + (i + j));

}

}

The output would be

i + j: 3

Here, two Integer objects are created and then + operation is performed on these integers as if they were primitive int types. Realizing that the Integer objects are being used as operands for an operator that performs on primitives, Java automatically converts the Integer object to int primitives and then performs the operation.

%d bloggers like this: