Conversion of String to other primitive data types

In this article, we will look at how to convert a String to the primitive data types – int, long, float and double.

Before going into this topic, we need to know a few details about wrapper classes. There are eight wrapper classes, one for each of the eight primitive data types. These wrapper classes are Byte, Short, Integer, Long, Float, Double, Character and Boolean. Each of these wrapper classes contains a static method (which you can call without creating an object of that class type) of the form parseDATATYPE() where data type should be replaced with the corresponding data type. These methods accept a String, convert it to the corresponding data type and return it.

For example, in the Integer class, the method is parseInt(). It can be used in the following way.

int result = Integer.parseInt(“34″);

The variable result will hold the int value 34. Note that there is a difference between “34″ and 34. “34″ is a String while 34 is an int.

The methods for int, long, float and double along with their usage is shown in the table below.

Wrapper Class Method Usage Result
Integer parseInt() int intVal = Integer.parseInt(“34″); 34
Long parseLong() long longVal = Long.parseLong(“34″); 34
Float parseFloat() float floatVal = Float.parseFloat(“3.4″); 3.4
Double parseDouble() double doubleVal = Double.parseDouble(“3.4″); 3.4

Following is a program illustrating the usage of the above methods:

public class Conversion {

    public static void main(String[] a) {
        int intVal = Integer.parseInt("34");
        long longVal = Long.parseLong("34");
        float floatVal = Float.parseFloat("3.4");
        double doubleVal = Double.parseDouble("3.4");
        System.out.println(intVal);
        System.out.println(longVal);
        System.out.println(floatVal);
        System.out.println(doubleVal);
    }

}

The output will be

34
34
3.4
3.4

valueOf() method

Each of the four wrapper classes discussed above also have another method by name valueOf() which takes a String converts it to an Integer, Long, Float or Double. Note that long is a primitive data type while Long is a class type used to wrap a long.

It’s usage is shown below for long type.

Long longObject = Long.valueOf(“34”);

longObject will hold an object of type Long which wraps the primitive long value 34.

There is a concept called as auto unboxing where you can assign a wrapper class object to a primitive type variable. By doing so, the wrapper class object will be automatically converted to a primitive type.

Consider the Long.valueOf() method.

long longVal = Long.valueOf(“34”);

longVal will hold 34.

The other methods are shown in the table below.

Method Return Type Can be assigned to Usage Result
Integer.valueOf(String) Integer int int intVal = Integer.valueOf(“34″); 34
Long.valueOf(String) Long long long longVal = Long.valueOf(“34″); 34
Float.valueOf(String) Float float float floatVal = Float.valueOf(“3.4″); 3.4
Double.valueOf(String) Double double double doubleVal = Double.valueOf(“3.4″); 3.4

 

Given below is a program illustrating the usage of the above methods.

public class Conversion {

    public static void main(String[] a) {
        int intVal = Integer.valueOf("34");
        long longVal = Long.valueOf("34");
        float floatVal = Float.valueOf("3.4");
        double doubleVal = Double.valueOf("3.4");
        System.out.println(intVal);
        System.out.println(longVal);
        System.out.println(floatVal);
        System.out.println(doubleVal);
    }

}

The output will be

34
34
3.4
3.4

Leave a Reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>