Java Program to convert string to integer

Let’s say the following is our string and we want to convert it to integer:

String s = "10"

Java has the built-in Integer.parseInt() method to convert string to integer. Following is an example:

Example 1

Converting string to integer using the parseInt() method in Java:

public class Example
{
   public static void main( String args[] )
   {
      String s = "10";
      
      System.out.println("String = "+s);
        
      System.out.println("Integer = "+Integer.parseInt(s));
   }
}

Output

String = 10
Integer = 10

Example 2

Converting string to integer using the valueOf() method in Java:

public class Example
{
   public static void main( String args[] )
   {
      String s = "25";
      
      System.out.println("String = "+s);
        
      System.out.println("Integer = "+Integer.valueOf(s));
   }
}

Output

String = 25
Integer = 25
Java Program to convert integer to string
Java Program to convert decimal to binary
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment