String myString = "1234";
int foo = Integer.parseInt(myString);

If you look at the Java documentation you'll notice the "catch" is that this function can throw a NumberFormatException, which you can handle:

int foo;
try {
   foo = Integer.parseInt(myString);
}
catch (NumberFormatException e) {
   foo = 0;
}

(This treatment defaults a malformed number to 0, but you can do something else if you like.)

Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8's Optional, makes for a powerful and concise way to convert a string into an int:

import com.google.common.primitives.Ints;

int foo = Optional.ofNullable(myString)
 .map(Ints::tryParse)
 .orElse(0)
Answer from Rob Hruska on Stack Overflow
🌐
Tutorialspoint
tutorialspoint.com › home › java › java integer parsing
Java Integer Parsing
September 1, 2008 - ... parseInt(int i) − This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
October 20, 2025 - The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.integer.parseint
Integer.ParseInt Method (Java.Lang) | Microsoft Learn
Parses the string argument as a signed decimal integer. [Android.Runtime.Register("parseInt", "(Ljava/lang/String;)I", "")] public static int ParseInt(string s);
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java integer parseint method
Java Integer parseInt Method
September 1, 2008 - The Java Integer parseInt(String s) method parses the string argument s as a signed decimal integer.
🌐
Study.com
study.com › courses › business courses › business 104: information systems and computer applications
How to Convert String to Int in Java - ParseInt Method - Lesson | Study.com
January 9, 2023 - For example, a string object could hold an integer value, and we might need to do some work with it as an integer. For this task, we have the parseInt() function. String q = new String("17"); int r = Integer.parseInt(q); System.out.println(r);
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 7 )
The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
🌐
Global Tech Council
globaltechcouncil.org › home › what is parseint in java?
What is parseInt in Java? - Global Tech Council
August 22, 2025 - Here’s are some examples of parseInt in Java: The most common use of parseInt() is to convert a string that represents a decimal number into an integer.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-valueof-vs-integer-parseint-with-examples
Integer.valueOf() vs Integer.parseInt() with Examples - GeeksforGeeks
July 11, 2025 - It takes a valid string as a parameter and parses it into primitive data type int. It only accepts String as a parameter and on passing values of any other data type, it produces an error due to incompatible types.
🌐
LabEx
labex.io › tutorials › java-java-integer-parseint-method-117728
Java parseInt(String, int) | Integer Parsing Tutorial | LabEx
Java parseInt(String s, int radix) method is used to parse a string value as a signed decimal Integer object in a specified integer radix value, which is part of the Integer class provided by the java.lang package.
🌐
Javatpoint
javatpoint.com › java-integer-parseint-method
Java Integer parseInt() Method - Javatpoint
Java Integer parseInt() Method - The Java parseInt() method is a method of Integer class that belong to java.lang package. The parseInt() method in Java is crucial for converting string representations of numbers into actual integer values. This capability is fundamental in various programming ...
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-integer-parseintmethod
Java Integer parseInt()Method
Here, we have two char sequences and we are parsing the part of the these sequences using parseInt() method of Integer class. public class JavaExample { public static void main(String[] args) { String s = "Hello1001Bye"; String s2 = "LEAF"; //taking 1001 part from string s int i = Integer.parseInt(s, 5, 9, 10); //taking EA from the string s2 and using 16 to parse it as hex int i2 = Integer.parseInt(s2, 1, 3, 16); System.out.println("int value: "+i); //Hex "EA" is equal to 234 in decimal System.out.println("int value: "+i2); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-string-to-int-in-java
String to int in Java - GeeksforGeeks
The most common method to convert a string to a primitive int is Integer.parseInt(). It throws a NumberFormatException if the string contains non-numeric characters. ... // Java Program to demonstrate // String to int conversion using parseInt() ...
Published   July 23, 2025
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › parseint in java
parseInt in Java: Everything You should Know | upGrad
June 25, 2025 - The answer lies in the parseInt() method in Java, a utility from the Integer class that converts a string into a primitive int.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 21 & JDK 21)
January 20, 2026 - The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 17 & JDK 17)
January 20, 2026 - The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
🌐
Scaler
scaler.com › home › topics › parseint() in java
parseInt() in Java - Scaler Topics
May 8, 2024 - It is used in Java for converting a string value to an integer by using the method parseInt().
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 11 & JDK 11 )
January 20, 2026 - The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.