Skip to content

August 7, 2014

Convert an Integer to String or from String to Integer in Java

by admin

Next example will convert an integer (number) to a string in Java:

package learn_java;
 
public class Learn_java {
 
    // convert integer to string
 
    public static void main(String[] args) {
        int number = 20;
        String str1 = number + "";
        System.out.println(str1);
 
        // or:
 
        String str2 = Integer.toString(number);
        System.out.println(str2);
    }   
}

We will use the following code to convert from string to int:

package learn_java;
 
public class Learn_java {
 
    // convert string to integer
 
    public static void main(String[] args) {
 
        String str1 = "200";
        String str2 = "300";
 
        int a, b;
 
        a = Integer.parseInt(str1);
        b = Integer.parseInt(str2);
 
        System.out.println(a+b);
    }   
}
Read more from Java Examples

Leave a Reply

required
required

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments