How to Convert String to Integer in Java


In this example we will show how to convert string to int in Java.

Source Code

1) Using Integer.parseInt()

package com.beginner.examples;

public class String2IntExample1 {

	public static void main(String[] args) {

		String number = "1";
		int i = Integer.parseInt(number);			
		System.out.println(i);
    }
}

Output:

1

2) Using Integer.valueOf()

package com.beginner.examples;

public class String2IntExample2 {

	public static void main(String[] args) {

		String number = "1";
		int i = Integer.valueOf(number).intValue();
		System.out.println(i);
    }
}

Output:

1
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments