Core Java

Wrapper Class Java Example

1. What is a wrapper class

A wrapper class in Java is a way to convert a primitive data type such as boolean, int, etc. into an object. Also, we can convert an object of a wrapper class into its corresponding primitive type.

In this article we are going to talk about why we need wrapper classes, autoboxing, and autounboxing. In addition, we will show you an example of creating and using a custom wrapper in Java.

The JDK version we use to compile the source code in this example is OpenJDK 13 and the IDE we use is Eclipse IDE 2020-03.

2. Why do we need wrapper classes?

Fundamentally Java is an object-oriented programming language and treats everything as an object. But primitive data types are not objects. They are built-in features of the Java language itself and they don’t belong to any class in any package. Several features provided by Java such as inheritance, polymorphism, and generics cannot deal with primitive data types directly but objects. For example, the Java collection framework we use very often works with objects only.

Furthermore, from a Clean Code perspective and by following coding best practices, a wrapper class lets developers write cleaner code, making it easier to read.

3. Primitive and corresponding wrapper class

The following table lists eight primitive types and their corresponding wrapper classes. These wrapper classes are part of java.lang package so the package name is omitted.

Primitive TypeWrapper Class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

4. Autoboxing

Autoboxing conversion, converts primitive type to its corresponding wrapper class. In the following scenarios, the autoboxing will be applied by the Java compiler:

  • A primitive is passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • A primitivee is assigned to a variable of the corresponding wrapper class.
AutoboxingExample.java

5. Autounboxing

Autounboxing, converts an object of a wrapper to its corresponding primitive value. In the following scenarios, the autounboxing will be applied by the Java compiler:

  • An object of a wrapper is passed as a parameter to a method that expects a value of the corresponding primitive type.
  • An object of a wrapper is assigned to a variable of the corresponding primitive type.
AutounboxingExample.java

6. A custom wrapper class Java example

Imagining we are working on an e-commerce website project and we need to get a new user’s age from the sign-up page. And also we need to read an existing user’s age from the database. What do these requirements mean? The age from user input on the sign-up page will be a text string. But the age read from the database normally is a primitive integer. Is it possible to design a Java class to handle both correctly? The answer is yes and a custom wrapper comes into play.

In the example below, we define a class Age. In addition to the default constructor, we define another constructor accepting a primitive int parameter. Also we define a static factory method valueOf which accepts a primitive int parameter and returns a new Age instance with the given value. To handle an age of String type, we can simply define another static factory method valueOf which accepts a String parameter and returns a new Age instance.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * A custom wrapper class represents Age.
 */
public class Age {
    // the variable holds the primitive age value
    private final int value;
 
    /**
     * Constructor.
     *
     * @param age
     */
    public Age(int age) {
        this.value = age;
    }
 
    /**
     * Returns an {@code Age} object holding the value of the specified {@code int}.
     *
     * @param age the age integer to be parsed.
     * @return an {@code Age} object holding the value represented by the integer argument.
     */
    public static Age valueOf(int age) {
        return new Age(age);
    }
 
    /**
     * Returns an {@code Age} object holding the value of the specified
     * {@code String}.
     *
     * @param age the age string to be parsed.
     * @return an {@code Age} object holding the value represented by the String argument.
     */
    public static Age valueOf(String age) {
        return new Age(Integer.valueOf(age));
    }
 
    /**
     * @return the value
     */
    public int intValue() {
        return value;
    }
 
    @Override
    public String toString() {
        return Integer.toString(value);
    }
}

Now let’s see how to use the Age class we just defined.

CustomWrapperClassExample.java

Is it possible for the Java compiler to apply autoboxing and autounboxing? Let’s have a try:

1
2
// Autoboxing for custom wrapper is not supported in Java
Age age0 = 0;

When we try to compile, it will fail with the following error message:

1
Type mismatch: cannot convert from int to Age.

So autoboxing and auto unboxing are not supported.

7. Download the Source Code

Download
You can download the full source code of this example here: Wrapper Class Java Example
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Kevin Yang

A software design and development professional with seventeen years’ experience in the IT industry, especially with Java EE and .NET, I have worked for software companies, scientific research institutes and websites.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button