Convert string to a number in Java


To convert a string to a number in Java, use the Integer.parseInt() method. First, let use create a String and set value.

String str = "45";

Now, take an Integer and use the Integer.parseInt() method.

Integer i = Integer.parseInt(str);

Let us see the complete example.

Example

 Live Demo

public class Demo {
   public static void main( String args[] ) {
      String str = "45";
      Integer i = Integer.parseInt(str);
      System.out.println("Num: " + i);
   }
}

Output

Num: 45

Updated on: 26-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements