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
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements