Java How to Convert String to int with Examples
In Java, you can convert a string to an int using
Integer.parseInt(yourString), which returns the integer value represented by the string.Examples
Input"123"
Output123
Input"0"
Output0
Input"-456"
Output-456
How to Think About It
To convert a string to an integer, think of the string as a number written in text form. You want to change it into a number type that Java can use for math. The method
Integer.parseInt() reads the string and turns it into an int if the string is a valid number.Algorithm
1
Take the input string that represents a number.2
Use a built-in method to read the string and convert it to an integer.3
Return or use the integer value for further operations.Code
java
public class Main { public static void main(String[] args) { String numberString = "123"; int number = Integer.parseInt(numberString); System.out.println(number); } }
Output
123
Dry Run
Let's trace converting the string "123" to an int using Integer.parseInt.
1
Input string
numberString = "123"
2
Convert string to int
number = Integer.parseInt("123") → 123
3
Print result
Output: 123
| Step | Operation | Value |
|---|---|---|
| 1 | Input string | "123" |
| 2 | Convert to int | 123 |
| 3 | Print output | 123 |
Why This Works
Step 1: Reading the string
The method Integer.parseInt() reads the characters in the string as digits.
Step 2: Converting to int
It converts those digits into an integer number type that Java can use for math.
Step 3: Using the int
The integer can then be stored in a variable or used in calculations.
Alternative Approaches
Using Integer.valueOf()
java
public class Main { public static void main(String[] args) { String numberString = "123"; int number = Integer.valueOf(numberString); System.out.println(number); } }
Returns an Integer object which is unboxed to int automatically; useful if you want an Integer object.
Using Scanner to read int from string
java
import java.util.Scanner; public class Main { public static void main(String[] args) { String numberString = "123"; Scanner scanner = new Scanner(numberString); int number = scanner.nextInt(); System.out.println(number); scanner.close(); } }
Useful when parsing input streams or more complex input, but more code than parseInt.
Complexity: O(n) time, O(1) space
Time Complexity
The method reads each character of the string once, so it takes time proportional to the string length, O(n).
Space Complexity
It uses a fixed amount of extra space, so space complexity is O(1).
Which Approach is Fastest?
Integer.parseInt() is the fastest and simplest for converting strings to int compared to alternatives like Integer.valueOf() or using Scanner.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Integer.parseInt() | O(n) | O(1) | Simple string to int conversion |
| Integer.valueOf() | O(n) | O(1) | When Integer object needed |
| Scanner.nextInt() | O(n) | O(1) | Parsing input streams or complex input |
Use
Integer.parseInt() for a simple and fast way to convert strings to int.Trying to convert a string that is not a valid number causes a
NumberFormatException.