Convert String to Wrapper Object Example

This example shows how to convert String to wrapper objects Integer, Long, Float, Double, Byte, and Short object.

How to convert String to wrapper objects in Java?

There are a couple of methods using which a string can be converted to wrapper class objects.

1) Using a wrapper class constructor

All wrapper classes provide a constructor that accepts a string as an argument and returns the respective wrapper object. You can use this constructor to convert as given in below example.

Output

2) Convert using the valueOf method of a wrapper class

You can use theĀ valueOf static method of a respective wrapper class to convert from string to any wrapper class object 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 *