Convert Primitive data type to Wrapper Object Example

This example shows how to convert Java primitive data types to respective wrapper class objects like int to Integer object, long to Long object, float to Float object, double to Double object, byte to Byte object, short to Short object and char to Character object.

How to convert a primitive data type to a wrapper object in Java?

There are a couple of ways in which Java primitive data types can be converted to respective wrapper objects.

1) Using a wrapper class constructor

Each wrapper class provides a constructor that accepts respective primitive value as an argument and creates a wrapper object around it as given in the below example.

Output

2) Using Autoboxing (Java 1.5 and later)

The autoboxing feature is introduced in Java 1.5 version which automatically converts a primitive value to the respective wrapper object automatically. That means, you can directly assign a primitive value or a variable to respective wrapper class object and conversion is done automatically as given below.

This example is a part of the Java Basic Examples and Java Type conversion Tutorial.

Please let me know your views in the comments section below.

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

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