Java String to int

Java String to int

We can convert String to int in java using Integer.parseInt() method.

Scenario

It is generally used if we have to perform mathematical operations on the string that contains number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is integer, we need to convert string to int. To do so, we use Integer.parseInt() method.

Signature

The parseInt() is the static method of Integer class. The signature of parseInt() method is given below:

  1. public static int parseInt(String s)

Java String to int Example

Let’s see the simple code to convert string to int in java.

  1. int i=Integer.parseInt(“200”);

Let’s see the simple example of converting String to int in java.

  1. public class StringToIntExample{
  2. public static void main(String args[]){
  3. String s=“200”;
  4. int i=Integer.parseInt(s);
  5. System.out.println(s+100);//200100 because + is string concatenation operator
  6. System.out.println(i+100);//300 because + is binary plus operator
  7. }}

Test it Now

Output:

200100
300

 

Next TopicJava int to String
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java String to int We can convert String to int in java using Integer.parseInt() method. Scenario It is generally used if we have to perform mathematical operations on the string that contains number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is integer, we need to convert string to […]

Tags:

....
...