Menu
Topics Index
...
`


Exploring java.lang > Primitive Type Wrappers >
Siva Nookala - 06 Oct 2016
In programming we will come across a situation where we need to convert number to string and string to number.

Converting Numbers to String:
There are many ways of converting numbers into string object,
  • By concatenating number with empty string i.e. " + " operator
  • The valueOf() method converts the given data type to String.
  • Each of the Number subclass includes a class method, toString, that will convert its primitive type in to a String.
Converting Numbers to String
class NumberstoStringTest
{
    public static void main(String arg[])
    {
        int i = 32;
        String s1 = "" + i; // LINE A
        System.out.println("s1 = " + s1);
        
        double d = 876.54;
        String s2 = String.valueOf(d); // LINE B
        System.out.println("s2 = " + s2);
        
        float f = 156.8f;
        String s3 = Float.toString(f); // LINE C
        System.out.println("s3 = " + s3);
        
        String s4 = "" + i + d; // LINE D
        
        System.out.println("s4 = " + s4);
        int n = s4.indexOf('.');
        System.out.println(n + " Digits before decimal point of s4 string.");
        System.out.println(s4.length() - n - 1
                + " Digits after decimal point of s4 string.");    
    }
}
OUTPUT

s1 = 32
s2 = 876.54
s3 = 156.8
s4 = 32876.54
5 Digits before decimal point of s4 string.
2 Digits after decimal point of s4 string.

DESCRIPTION

In this program, at LINE A, int is converted into String by concatenating with empty string,
At LINE B double is converted into String using valueOf method,
At LINE C float is converted into String using toString method and finally at LINE D, we are concatenating empty String, integer and double.
We are counting number of digits before and after the decimal point, in the resultant String.

THINGS TO TRY
  • Apply valueOf method to different data types as String.valueOf(datatype).
  • Apply toString method to all the data types as Integer.toString(integer_Datatype), Double.toString(double_datatype) etc.,
  • Try to perform operations on the resultant strings.

Converting Strings to Number:
The two ways of converting Strings into specified data type are,
  • The valueOf() method converts the String to specified data type.
  • The parsing methods are widely used to convert strings into numbers.
Converting Strings to Number
class StringstoNumberTest
{
    public static void main(String arg[])
    {
        String s1 = "25";
        int i1 = Integer.valueOf(s1); // LINE E
        int i2 = Integer.parseInt(s1); // LINE F
        System.out.println(i1 + " " + i2);
        
        float f1 = Float.valueOf(s1);
        float f2 = Float.parseFloat(s1);
        System.out.println(f1 + " " + f2);
        
        double d1 = Double.valueOf(s1);
        double d2 = Double.parseDouble(s1);
        System.out.println(d1 + " " + d2);
        
        System.out.print("The sum of all numbers is : ");
        System.out.println(i1 + i2 + f1 + f2 + d1 + d2); // LINE G
        
        String s2 = "ABCD";
        char c1[] = new char[s2.length()];
        for (int i = 0; i < s2.length(); i++) {
            c1[i] = Character.valueOf(s2.charAt(i)); // LINE H
        }
        for (int j = 0; j < c1.length; j++) {
            System.out.print(c1[j] + " ");
        }    
    }
}
OUTPUT

25 25
25.0 25.0
25.0 25.0
The sum of all numbers is : 150.0
A B C D

DESCRIPTION

In this program, at LINE E and LINE F, string s1 is converted into integer by using valueOf, parseInt methods respectively. Similarly float and double are converted from string s1. At LINE G all 6 numbers sum is printed. At LINE H the string s2 containing ABCD is converted into character array and all characters are displayed.

THINGS TO TRY
  • Try for the below code.
    double d = Double.parseDouble("10");
    System.out.println(d);
    The output should be 10.0, since we are using parseDouble and it is assigned to datatype double.
  • Try for the below code.
    int i = Integer.parseInt("1010", 2);
    System.out.println(i);
    The output will be 10, since here the paseInt method returns an integer value of the given string representation of binary radix.
    Try parseInt(String s , int radix) for decimal, Octal and Hexadecimal radices
.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App