Convert string to a number in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 13-Nov-2018 07:27:46
Advertisements