Skip to content

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

Leave a Reply

Your email address will not be published.