java tutorial - How to convert string to integer in java - java programming - learn java - java basics - java for beginners



 str to int1

Learn Java - Java tutorial - str to int1 - Java examples - Java programs

Using Integer.parseInt()

  • The Integer.parseInt() method is used primarily in parsing a String method argument into Integer object.
  • The Integer.parseInt method is a very convenient Java API to transform a user input in String format into a more robust object type which in this case the Integer.
  • Make a note that the parseInt method of Integer class is static hence it must be accessed statically. Mean to say that we would calling this method.

Syntax

Using Integer.parseInt()

Sample Code:

public class Str_to_Int {
    public static void main(String[] args) {
        String num= "108";
             int val = Integer.parseInt(num);
               System.out.println(val);
    }
}

Output:

108
 str to int2

Learn Java - Java tutorial - str to int2 - Java examples - Java programs

Java convert string to int Using Integer.valueOf()

  • Integer.valueOf() Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.

Syntax

Using Integer.valueOf()

Sample Code:

public class Str_to_Int {
    public static void main(String[] args) {
        String num= "108";
             Integer val = Integer.valueOf(num);
             System.out.println(val);
    }
}

Output:

            108

Related Searches to How to convert string to integer in java