Java Program to Convert String to Int

This Java program converts String to Integer.

Usually String to int conversion is required when we need to do some mathematical operations in the string variable that contains numbers. This situation normally arises whenever we receive data from user input via textfield or textarea, data is obtained as string and we required to convert string to int.

Java Integer class has parseInt() static method, which is used convert string to int.

Syntax:
public static int parseInt(String s)
Example:
public class StrToInt {

 public static void main(String args[]) {
  String str = "123";
  int i = Integer.parseInt(str);
  System.out.println(str + 50); //12350 because its concatenate to value.
  System.out.println(i + 50); //173 because +(plus) is binary plus operator. 
 }

}
Program Output:
12350
173

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.