How to convert String to Wrapper,String to Primitive, Primitive to String, Primitive to Wrapper, Wrapper to String and Wrapper to Primitive in Java

In this tutorial we will see the conversion of String <-> Wrapper <-> Primitive in java. When we talk about conversion,  Java includes some predefined methods to perform conversion. We will see those methods with some examples.

How to convert String to Wrapper Object:

To convert String to Wrapper we have valueOf() method in java. It’s a static method. Also it might throw an Number Format Exception (NFE) if we try to  convert some unrecognizable String to Wrapper Object. For example if we try to convert “six” to 6 then it will throw Number Format Exception (NFE). Using valueOf() method we can convert String to Boolean, Byte, Double, Float, Integer, Long and Short Wrapper object. But we can’t convert String to Character object.

Let’s see with an example

package StringtoWrapper;

public class StringtoWrapper {

    private static void StringtoBoolean() {
        // TODO Auto-generated method stub
        System.out.println("String to Boolean");
        System.out.println("-----------------");
        System.out.println(Boolean.valueOf("FAlse"));
        System.out.println(Boolean.valueOf("TrUe"));
        System.out.println(Boolean.valueOf("trUe"));
        System.out.println(Boolean.valueOf("false"));
        System.out.println(Boolean.valueOf("learnfromexamples"));
        System.out.println("-----------------");
    }
    private static void StringtoByte() {
        System.out.println("String to Byte");
        System.out.println("-----------------");
        // TODO Auto-generated method stub
        // Byte Range will be from -128 to 127
        System.out.println(    Byte.valueOf("12"));   //Line 1
        System.out.println(    Byte.valueOf("-128")); //Line 2
        System.out.println(    Byte.valueOf("-127")); //Line 3
        //System.out.println(Byte.valueOf("fds"));  //Line 4
        //System.out.println(Byte.valueOf("-129")); //Line 5
        //System.out.println(Byte.valueOf("130")); //Line 6
        System.out.println("-----------------");
    }
    private static void StringtoDouble() {
        System.out.println("String to Double");
        System.out.println("-----------------");
        // TODO Auto-generated method stub
        System.out.println(Double.valueOf("165.42"));  //Line 1
        System.out.println(Double.valueOf("462"));       //Line 2    
        //System.out.println(Double.valueOf("learnfromexamples")); //Line 3
        System.out.println("-----------------");
    }
    
    private static void StringtoFloat() {
        System.out.println("String to Float");
        System.out.println("-----------------");
        // TODO Auto-generated method stub
        System.out.println(Float.valueOf("100.42f"));  //Line 1
        System.out.println(Float.valueOf("4232"));       //Line 2
        System.out.println(Float.valueOf("4619f"));       //Line 3
        //System.out.println(Double.valueOf("learnfromexamples")); //Line 4
        System.out.println("-----------------");
    }
    public static void StringtoInteger(){
        System.out.println("String to Integer");
        System.out.println("-----------------");
        System.out.println(Integer.valueOf("1065")); //Line 1
        System.out.println(Integer.valueOf("3454")); //Line 2
        //System.out.println(Double.valueOf("learnfromexamples")); //Line 3
        System.out.println("-----------------");
    }
    public static void StringtoLong(){
        System.out.println("String to Long");
        System.out.println("-----------------");
        System.out.println(Long.valueOf("1234")); //Line 1
        System.out.println(Long.valueOf("342")); //Line 2
        //System.out.println(Long.valueOf("learnfromexamples")); //Line 3
        System.out.println("-----------------");
    }
    public static void StringtoShort(){
        System.out.println("String to Short");
        System.out.println("-----------------");
        System.out.println(Short.valueOf("195")); //Line 1
        System.out.println(Short.valueOf("348")); //Line 2
        //System.out.println(Short.valueOf("learnfromexamples")); //Line 3
        System.out.println("-----------------");
    }
    public static void main(String[] args) {
        StringtoBoolean();
        StringtoByte();
        StringtoDouble();
        StringtoFloat();
        StringtoInteger();
        StringtoLong();
        StringtoShort();
    }
    
}

Output

String to Boolean
-----------------
false
true
true
false
false
-----------------
String to Byte
-----------------
12
-128
-127
-----------------
String to Double
-----------------
165.42
462.0
-----------------
String to Float
-----------------
100.42
4232.0
4619.0
-----------------
String to Integer
-----------------
1065
3454
-----------------
String to Long
-----------------
1234
342
-----------------
String to Short
-----------------
195
348
-----------------

 

Explanation for StringtoBoolean() method:

If you see the output for StringtoBoolean() method, even though the values(FAlse, TrUe, trUe, false) we are passing to valueOf() method are case-insensitive, it recognizes the string and perform conversion. But when we pass some other string (learnfromexamples) to valueOf() instead of throwing Number Format Exception (NFE) it converts into a false Wrapper Object.

Explanation for StringtoByte() method:

If you see the output for StringtoByte() method, it will convert the String to Byte Wrapper and print the values, But if we uncomment any one of the Line 4, Line 5 or Line 6 and execute the program it will throw NFE exception, because if we see the Line 4, “fds” is a String and it is not recognized by Byte so it will throw NFE. Also in Line 5 and Line 6, “-129” and “130” is not in a range of Byte so it throws NFE.

Note: Byte will allows the values between “-128 to 127”

Explanation for StringtoDouble() method:

If you see the output for StringtoDouble() method, it will convert the String to Double Wrapper object and print the values, But if we uncomment the Line 3 and execute the program it will throw NFE exception, because if you see the Line 3, “learnfromexamples” is a String and it is not recognized by Double so it will throw NFE.

Explanation for StringtoFloat() method:

If you see the output for StringtoFloat() method, it will convert the String to Float Wrapper object and print the values, But if we uncomment the Line 4 and execute the program it will throw NFE exception, because if you see the Line 4, “learnfromexamples” is a String and it is not recognized by Float so it will throw NFE.

Similarly StringtoInteger(), StringtoLong() and StringtoShort() methods will convert String to its corresponding Wrapper class object.

How to convert Primitive to Wrapper object:

To convert Primitive to Wrapper, we have Constructors for all primitives (i.e new Boolean(), new Byte() etc). So we just have to pass the primitive values to the constructor and get the corresponding Wrapper object.

package StringtoWrapper;

public class PrimitivetoWrapper {
  public static void main(String[] args) {
          primitivetoBoolean();
          primitivetoByte();
          primitivetoDouble();
          primitivetoFloat();
          primitivetoInteger();
          primitivetoShort();
          primitivetoLong();
  }

  private static void primitivetoLong() {
          // TODO Auto-generated method stub
          System.out.println("Primitive to Long");
          System.out.println("------------------");
          Long long1 = new Long(21);
          System.out.println(long1);
          System.out.println("==================");
  }

  private static void primitivetoShort() {
          // TODO Auto-generated method stub
          System.out.println("Primitive to Short");
          System.out.println("------------------");
          Short short1 = new Short((short) 33);
          System.out.println(short1);
          System.out.println("==================");
  }

  private static void primitivetoInteger() {
          // TODO Auto-generated method stub
          System.out.println("Primitive to Integer");
          System.out.println("------------------");
          Integer integer = new Integer(234);
          System.out.println(integer);
          System.out.println("==================");
}

  private static void primitivetoFloat() {
          // TODO Auto-generated method stub
          System.out.println("Primitive to Float");
          System.out.println("------------------");
          Float float1 = new Float(12.443);
          System.out.println(float1);
          System.out.println("==================");
  }

  private static void primitivetoDouble() {
          // TODO Auto-generated method stub
          System.out.println("Primitive to Double");
          System.out.println("------------------");
          Double double1 = new Double(23.4);
          System.out.println(double1);
          System.out.println("==================");
  }

  private static void primitivetoByte() {
          System.out.println("Primitive to Byte");
          System.out.println("------------------");
          // TODO Auto-generated method stub
          Byte byte1 = new Byte((byte) 16);
          System.out.println(byte1);
          System.out.println("==================");
  }

  private static void primitivetoBoolean() {
          System.out.println("Primitive to Boolean");
          System.out.println("------------------");
          // TODO Auto-generated method stub
          Boolean boolean1 = new Boolean(true);
          System.out.println(boolean1);
          Boolean boolean2 = new Boolean(false);
          System.out.println(boolean2);
          System.out.println("==================");
  }
}

Output


Primitive to Boolean
------------------
true
false
==================
Primitive to Byte
------------------
16
==================
Primitive to Double
------------------
23.4
==================
Primitive to Float
------------------
12.443
==================
Primitive to Integer
------------------
234
==================
Primitive to Short
------------------
33
==================
Primitive to Long
------------------
21
==================

How to convert Wrapper object to Primitive:

To convert Wrapper to primitive we have booleanValue(), byteValue(), doubleValue(), floatValue(), integerValue(), longValue(), shortValue() methods in java. These methods will throw Number Format Exception (NFE) if we try to convert some unrecognizable Wrapper to primitive. Similar to valueOf() method these methods will also can’t convert Wrapper to char.


package StringtoWrapper;

public class WrappertoPrimitive {

  private static void Wrappertoboolean() {
        System.out.println("Wrapper to boolean");
        System.out.println("------------------");
        // TODO Auto-generated method stub
        Boolean boolean1 = new Boolean(true);
        boolean flag1 = boolean1.booleanValue();
        System.out.println(flag1);

        Boolean boolean2 = new Boolean(false);
        boolean flag2 = boolean2.booleanValue();
        System.out.println(flag2);

        Boolean boolean4 = new Boolean("tRue");
        boolean flag4 = boolean4.booleanValue();
        System.out.println(flag4);

        Boolean boolean3 = new Boolean("learn from examples");
        boolean flag3 = boolean3.booleanValue();
        System.out.println(flag3);
        //System.out.println("------------------");
        System.out.println("==================");
  }
  private static void Wrappertodouble(){
        System.out.println("Wrapper to double");
        System.out.println("-----------------");
        Double double1 = new Double(23.4);
        double d1 = double.doubleValue();
        System.out.println(d1);

        Double double2 = new Double("2132.4");
        double d2 = double2.doubleValue();
        System.out.println(d2);
        System.out.println("====================");
  }
  private static void Wrappertobyte() {
        System.out.println("Wrapper to byte");
        System.out.println("------------------");
        // TODO Auto-generated method stub
        Byte byte1 = new Byte("123");
        byte b = byte1.byteValue();
        System.out.println(b);

        /*
        * Byte byte2 = new Byte("234");
        byte b1 = byte2.byteValue();
        System.out.println(b1);

        Byte byte3 = new Byte("learnfromexamples");
        byte b3 = byte3.byteValue();
        System.out.println(b3);*/
        System.out.println("==================");
  }

  private static void Wrappertofloat() {
        System.out.println("Wrapper to float");
        System.out.println("------------------");
        // TODO Auto-generated method stub
        Float float1 = new Float(213.23);
        float f = float1.floatValue();
        System.out.println(f);

        Float float2 = new Float(13.23f);
        float f1 = float2.floatValue();
        System.out.println(f1);

        Float float3 = new Float("13.23f");
        float f2 = float3.floatValue();
        System.out.println(f2);

        System.out.println("==================");
  }

  private static void Wrappertointeger() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to integer");
        System.out.println("------------------");
        Integer integer = new Integer(234);
        int i = integer.intValue();
        System.out.println(i);

        Integer integer1 = new Integer("234");
        int i1 = integer1.intValue();
        System.out.println(i1);
        System.out.println("==================");
  }

  private static void Wrappertoshort() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to short");
        System.out.println("------------------");
        Short short1 = new Short((short) 34);
        int s = short1.intValue();
        System.out.println(s);

        Short short2 = new Short("234");
        int s1 = short2.intValue();
        System.out.println(s1);
        System.out.println("==================");
  }

  private static void Wrappertolong() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to long");
        System.out.println("------------------");
        Long long1 = new Long(34);
        int l = long1.intValue();
        System.out.println(l);

        Long long2 = new Long("234");
        int l1 = long2.intValue();
        System.out.println(l1);
        System.out.println("==================");
  }

  public static void main(String[] args) {
        // TODO Auto-generated method stub
        Wrappertoboolean();
        Wrappertobyte();
        Wrappertodouble();
        Wrappertofloat();
        Wrappertointeger();
        Wrappertoshort();
        Wrappertolong();
  }
}

Output

Wrapper to boolean
------------------
true
false
true
false
==================
Wrapper to byte
------------------
123
==================
Wrapper to double
------------------
23.4
2132.4
==================
Wrapper to float
------------------
213.23
13.23
13.23
==================
Wrapper to integer
------------------
234
234
==================
Wrapper to short
------------------
34
234
==================
Wrapper to long
------------------
34
234
==================

Explanation:

If you see the output for Wrappertoboolean() method, we have passed String as well as Boolean value in the constructor and form a Wrapper class. And using booleanValue() we are converting into the boolean primitive value. But when we pass some other string (learnfromexamples) to instead of throwing Number Format Exception (NFE) it converts into a false Wrapper Object. Similar to this, it will do conversion  for other methods also. In this class  I have also commented some codes, if you uncomment those lines and execute it, then it will throw NFE.

How to convert String object to Primitive:

To convert String to primitive we have parseBoolean(), parseByte(), parseDouble(), parseFloat(), parseInteger, parseLong(), parseShort() methods in java. These methods will throw Number Format Exception (NFE) if we try to convert some unrecognizable string to primitive.Similar to valueOf() method these methods will also can’t convert String to char.

package StringtoWrapper;

public class StringtoPrimitive {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Stringtoboolean();
        Stringtobyte();
        Stringtodouble();
        Stringtofloat();
        Stringtointeger();
        Stringtolong();
        Stringtoshort();
    }

    private static void Stringtoshort() {
        // TODO Auto-generated method stub
        System.out.println("String to short");
        System.out.println("------------------");
        short s = Short.parseShort("3");
        System.out.println(s);
        /*short s1 = Short.parseShort("learn from examples");
        System.out.println(s1);*/
        System.out.println("==================");
    }

    private static void Stringtolong() {
        // TODO Auto-generated method stub
        System.out.println("String to long");
        System.out.println("------------------");
        long l = Long.parseLong("3253");
        System.out.println(l);
        /*long l1 = Long.parseLong("learn from examples");
        System.out.println(l1);*/
        System.out.println("==================");
    }

    private static void Stringtointeger() {
        // TODO Auto-generated method stub
        System.out.println("String to integer");
        System.out.println("------------------");
        int i = Integer.parseInt("100");
        System.out.println(i);
        /*int i1 = Integer.parseInt("learn from examples");
        System.out.println(i1);*/
        System.out.println("==================");    
    }

    private static void Stringtofloat() {
        // TODO Auto-generated method stub
        System.out.println("String to float");
        System.out.println("------------------");
        float f = Float.parseFloat("23.5");
        System.out.println(f);
        float f1 = Float.parseFloat("123.5f");
        System.out.println(f1);
        /*float f2 = Float.parseFloat("learn from examples");
        System.out.println(f2);*/
        System.out.println("==================");
    }

    private static void Stringtodouble() {
        // TODO Auto-generated method stub
        System.out.println("String to double");
        System.out.println("------------------");
        double d = Double.parseDouble("23.5");
        System.out.println(d);
        /*double d1 = Double.parseDouble("learn from examples");
        System.out.println(d1);*/
        System.out.println("==================");
    }

    private static void Stringtobyte() {
        // TODO Auto-generated method stub
        System.out.println("String to byte");
        System.out.println("------------------");
        byte b = Byte.parseByte("2");
        System.out.println(b);
        /*byte b1 = Byte.parseByte("learn from examples");
        System.out.println(b1);*/
        System.out.println("==================");
    }

    private static void Stringtoboolean() {
        // TODO Auto-generated method stub
        System.out.println("String to boolean");
        System.out.println("------------------");
        boolean b = Boolean.parseBoolean("true");
        System.out.println(b);
        boolean b1 = Boolean.parseBoolean("false");
        System.out.println(b1);
        boolean b2 = Boolean.parseBoolean("learnfromexamples");
        System.out.println(b2);
        System.out.println("==================");
    }

}

Output

String to boolean
------------------
true
false
false
==================
String to byte
------------------
2
==================
String to double
------------------
23.5
==================
String to float
------------------
23.5
123.5
==================
String to integer
------------------
100
==================
String to long
------------------
3253
==================
String to short
------------------
3
==================

 How to convert Primitive to String object:

To convert Primitive to String all Wrapper class will have toString() method. it’s a static method.

 

package StringtoWrapper;

public class PrimitivetoString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        primitivetoBoolean();
        primitivetoByte();
        primitivetoDouble();
        primitivetoFloat();
        primitivetoInteger();
        primitivetoLong();
        primitivetoShort();
    }

    private static void primitivetoShort() {
        // TODO Auto-generated method stub
        System.out.println("Primitive to Short");
        System.out.println("--------------------");
        System.out.println(Short.toString((short) 32));
        System.out.println("===================");
    }

    private static void primitivetoLong() {
        // TODO Auto-generated method stub
        System.out.println("Primitive to Long");
        System.out.println("--------------------");
        System.out.println(Long.toString(235));
        System.out.println("===================");
    }

    private static void primitivetoInteger() {
        // TODO Auto-generated method stub
        System.out.println("Primitive to Integer");
        System.out.println("--------------------");
        System.out.println(Integer.toString(23));
        System.out.println("===================");
    }

    private static void primitivetoFloat() {
        // TODO Auto-generated method stub
        System.out.println("Primitive to Float");
        System.out.println("--------------------");
        System.out.println(Float.toString(12.3f));
        System.out.println("===================");
    }

    private static void primitivetoDouble() {
        // TODO Auto-generated method stub
        System.out.println("Primitive to Double");
        System.out.println("--------------------");
        System.out.println(Double.toString(24.34));
        System.out.println("===================");
    }

    private static void primitivetoByte() {
        // TODO Auto-generated method stub
        System.out.println("Primitive to Byte");
        System.out.println("--------------------");
        System.out.println(Byte.toString((byte) 3));
        System.out.println("===================");
    }

    private static void primitivetoBoolean() {
        // TODO Auto-generated method stub
        System.out.println("Primitive to Boolean");
        System.out.println("--------------------");
        String boolean1 = Boolean.toString(true);
        System.out.println(boolean1);

        String boolean2 = Boolean.toString(false);
        System.out.println(boolean2);
        System.out.println("===================");
    }

}

Primitive to Boolean
--------------------
true
false
===================
Primitive to Byte
--------------------
3
===================
Primitive to Double
--------------------
24.34
===================
Primitive to Float
--------------------
12.3
===================
Primitive to Integer
--------------------
23
===================
Primitive to Long
--------------------
235
===================
Primitive to Short
--------------------
32
===================

How to convert Wrapper to String object:

Eventhough all Wrapper class have static toString() methods, apart from that it will also have an instance version of toString() which will convert Wrapper to String object.

package StringtoWrapper;

public class WrappertoString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WrappertoBoolean();
        WrappertoByte();
        WrappertoDouble();
        WrappertoFloat();
        WrappertoInteger();
        WrappertoLong();
        WrappertoShort();
    }

    private static void WrappertoShort() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to Short");
        System.out.println("--------------------");
        Short short1 = new Short((short) 3);
        System.out.println(short1.toString());
        System.out.println("===================");
    }

    private static void WrappertoLong() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to Long");
        System.out.println("--------------------");
        Long long1 = new Long(2341);
        System.out.println(long1.toString());
        System.out.println("===================");
    }

    private static void WrappertoInteger() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to Integer");
        System.out.println("--------------------");
        Integer integer = new Integer(323);
        System.out.println(integer.toString());
        System.out.println("===================");
    }

    private static void WrappertoFloat() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to Float");
        System.out.println("--------------------");
        Float float1 = new Float(12.3);
        System.out.println(float1.toString());
        System.out.println("===================");
    }

    private static void WrappertoDouble() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to Double");
        System.out.println("--------------------");
        Double double1 = new Double(223.45);
        System.out.println(double1.toString());
        System.out.println("===================");
    }

    private static void WrappertoByte() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to Byte");
        System.out.println("--------------------");
        Byte byte1 = new Byte((byte) 32);
        System.out.println(byte1.toString());
        System.out.println("===================");
    }

    private static void WrappertoBoolean() {
        // TODO Auto-generated method stub
        System.out.println("Wrapper to Boolean");
        System.out.println("--------------------");
        Boolean boolean1 = new Boolean(true);
        System.out.println(boolean1.toString());

        Boolean boolean2 = new Boolean(false);
        System.out.println(boolean2.toString());
        System.out.println("===================");
    }

}

Wrapper to Boolean
--------------------
true
false
===================
Wrapper to Byte
--------------------
32
===================
Wrapper to Double
--------------------
223.45
===================
Wrapper to Float
--------------------
12.3
===================
Wrapper to Integer
--------------------
323
===================
Wrapper to Long
--------------------
2341
===================
Wrapper to Short
--------------------
3
===================

4 thoughts on “How to convert String to Wrapper,String to Primitive, Primitive to String, Primitive to Wrapper, Wrapper to String and Wrapper to Primitive in Java

Leave a comment

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