Java Basic Examples

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 primitive data type to 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 Wrapper class constructor

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


2) Using Autoboxing (Java 1.5 and later)

Autoboxing feature is introduced in Java 1.5 version which automatically converts a Java primitive value to 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.

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

About the author



rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

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