Wrapper Classes in Java

In this post, we will discuss the wrapper classes of Java in detail.

  • Wrapper classes provide a mechanism to “wrap” primitive values in an object so that primitives can be used for the operations reserved for the objects, like being used in a map or Collections.
  • The wrapper classes can be used in the process of serialization and deserialization.
  • Wrapper objects are immutable, meaning they can’t be changed once created.
  • Wrapper classes also provide us with many utility functions that can be used with primitives.

There is a wrapper class for every primitive in Java. For example, the wrapper class for int is Integer, the class for float is Float, and so on.

PrimitiveWrapper class
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
byteByte
booleanBoolean

How to make Objects of a wrapper class?

There are two ways by which we create objects of a wrapper class.

Using constructors
  • There are constructors for each of the Wrapper classes where we can pass their corresponding primitive type values and some other data types for some of the wrapper classes.
  • There are no default constructors for any of the wrapper classes

The table below illustrates the arguments we can pass to a wrapper class constructor while making their objects.

Wrapper classConstructor Arguments
Shortshort or String
Integerint or String
Longlong or String
Floatfloat, double, or String
Doubledouble or String
Characterchar
Bytebyte or String
Booleanboolean or String

The above table shows that the Integer type object can be made using an int primitive and a String object. Please remember that we cannot pass the string as “two” and hope that it will convert to 2 automatically. This would result in a NumberFormatException. But we can pass the string like “2” to the Integer type constructor, which would work fine.

Integer i = new Integer(2)  // this is correct
Integer i = new Integer("2") // this is also correct
Integer i = new Integer("two") // this will throw a "NumberFormatException"

Using valueOf() method

Every wrapper class has a static valueOf() method, which generally accepts its corresponding primitive type value or a String object.

public class Codekru {

	public static void main(String[] args) throws Exception {

		Integer i = Integer.valueOf(5);
		Integer j = Integer.valueOf("6");

		System.out.println("Integer i = " + i);
		System.out.println("Integer j = " + j);

	}
}

Output –

Integer i = 5
Integer j = 6
Which method is more preferred to make objects? Using constructors or using valueOf() method?

Since Java 9, making wrapper class objects through constructors is deprecated, and thus the use of the valueOf() method while making wrapper class objects is preferred. The same is also mentioned in the Java documentation.

It is rarely appropriate to use this constructor. The static factory valueOf is generally a better choice, as it is ikely to yield significantly better space and time performance.

So, we will also encourage you to use the valueOf() method to create wrapper class objects.

Autoboxing and Unboxing

Java 5 introduced new features – autoboxing and unboxing.

  • Autoboxing – The automatic conversion of primitive types to their corresponding wrapper classes is called autoboxing. Like int converted to Integer class, float converted to Float class, etc.
  • Unboxing – Similarly, the automatic conversion of a wrapper type to its corresponding primitive type is called unboxing. Like Integer to int, Float type to float, and so on
Autoboxing and unboxing

Let’s see an example of autoboxing.

public class Codekru {

	public static void autoboxing(Integer i) {
		System.out.println("Integer passed: " + i);
	}

	public static void main(String[] args) throws Exception {

		int i = 3;
		autoboxing(i);

	}
}

Output –

Integer passed: 3

We passed an int primitive type to the autoboxing() function, which only accepts an argument of the Integer type. So, the automatic conversion of primitive type to its wrapper class happened ( autoboxing ), and the program worked fine.

As we mentioned, autoboxing and unboxing were introduced in Java 5, whereas the wrapper classes existed long before. So, before Java 5, the developer has to write the code to make the wrapper, unwrap it, use it, and then rewrap it.

 Integer i = Integer.valueOf(8);  // making the wrapper
int j = i.intValue();     // unwrapping it
j++;                     // using it
i = new Integer(j);     // re-wrapping it

But with java 5 and the features of autoboxing and unboxing, all this can be done elegantly and efficiently.

Integer i = Integer.valueOf(8);  // making the wrapper
i++;       // unwrapping it, using it 
          //and then re-wrapping it

Now, let’s see an example of unboxing.

public class Codekru {

	public static void unboxing(int i) {
		System.out.println("primitive int value: " + i);
	}

	public static void main(String[] args) throws Exception {

		Integer i = Integer.valueOf(5);
		unboxing(i);

	}
}

Output –

primitive int value: 5

Wrapper class objects and immutability

The Wrapper class’s objects are immutable. Immutable means the one that cannot be changed. So, if we have created an object of the Integer wrapper class representing an integer value, we cannot change the value of that object. However, we can change the pointing of that object to represent a new integer value.

public class Codekru {

	public static void main(String[] args) throws Exception {

		Integer i = Integer.valueOf(1000);
		Integer j = i;

		if (i == j) {
			System.out.println("Both referrring to same object");
		} else {
			System.out.println("Both are referring to different objects");
		}

		i++; // this will create a new object with value 1001

		System.out.println("After incrementing values");
		if (i == j) {
			System.out.println("Both referrring to same object");
		} else {
			System.out.println("Both are referring to different objects");
		}

	}
}

Output –

Both referrring to same object
After incrementing values
Both are referring to different objects

If we give you a diagrammatical representation, it would be something like this.

Wrapper classes are immutable

We hope that you have liked the article. If you have any doubts or concerns, please feel free to reach us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

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