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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
July 21, 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.
People also ask

How do I convert a String to an int in Java?
You can convert a String to an int in Java using Integer.parseInt(string) or Integer.valueOf(string). The parseInt() method returns a primitive int, while valueOf() returns an Integer object which can be auto-unboxed to int.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string to int in java
How to Convert String to int in Java: Methods and Examples
How to convert int to String in Java with leading zeros?
You can use String.format() to create a string with leading zeros: int number = 42;String withLeadingZeros = String.format("%05d", number); // "00042" int number = 42 ; String withLeadingZeros = String . format ( "%05d" , number ) ; // "00042"
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string to int in java
How to Convert String to int in Java: Methods and Examples
What's the difference between parseInt() and valueOf() for String to int conversion?
The main difference is that parseInt() returns a primitive int, while valueOf() returns an Integer object. In terms of functionality, valueOf() internally calls parseInt() and wraps the result in an Integer object. For caching, valueOf() caches Integer objects for values between -128 and 127.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string to int in java
How to Convert String to int in Java: Methods and Examples
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-a-string-to-an-int-in-java
How to Convert a String to an int in Java? - GeeksforGeeks
July 12, 2025 - // Java Program to convert String to int public class StringToInt { public static void main(String[] args) { String s = "123"; // Convert string to int using parseInt int n = Integer.parseInt(s); System.out.println("The integer value is: " + n); } }
🌐
Baeldung
baeldung.com › home › java › java string › convert string to int or integer in java
Convert String to int or Integer in Java | Baeldung
December 15, 2023 - In this article we will show multiple ways of converting a String to an int or Integer.
🌐
Mkyong
mkyong.com › home › java › how to convert string to integer in java
How to convert String to Integer in Java - Mkyong.com
January 26, 2022 - package com.mkyong.string; public class ConvertStringToInteger { public static void main(String[] args) { String number = "D99"; try { Integer result = Integer.valueOf(number); System.out.println(result); } catch (NumberFormatException e) { //do something for the exception. System.err.println("Invalid number format : " + number); } } } Below is a Java 8 example of converting a String to an Integer object and returning an Optional<Integer>.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-string-to-int-in-java
String to int in Java - GeeksforGeeks
The Integer.valueOf() method converts a String to an Integer object instead of a primitive int. We can unbox it to an int. Note: valueOf() method uses parseInt() internally to convert to integer.
Published: July 23, 2025
🌐
HappyCoders.eu
happycoders.eu › java › convert-string-to-int-peculiarities-pitfalls
How to Convert String to Int in Java – Peculiarities and Pitfalls
December 2, 2024 - Let's first look at the options to parse a String into an int (or Integer). Up to Java 7, we have these two methods:
Find elsewhere
🌐
Mkyong
mkyong.com › home › java › how to convert string to int in java
How to convert String to int in Java - Mkyong.com
January 26, 2022 - The Integer.parseInt(String) convert a String to primitive type int; The Integer.valueOf(String) convert a String to a Integer object. For unparsable String, both methods throw NumberFormatException. int result1 = Integer.parseInt("1"); Integer ...
🌐
IONOS
ionos.com › digital guide › websites › web development › java string to int
How to convert a string to int in Java - IONOS
January 2, 2025 - The numerical value of the string is: 7312 `` However, this only works if the string exclusively **contains whole numbers**. If it contains anything else, you’ll get an error message: ```java public class Main { public static void main(String args[]) { String str = "The number is 7312"; int number = Integer.parseInt(str); System.out.println("The number from the string is: " + number); } }java
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string to int in java
How to Convert String to int in Java: Methods and Examples
May 13, 2025 - Learn how to convert String to int in Java with multiple methods including parseInt, valueOf, and avoiding common errors with practical examples.
🌐
Baeldung
baeldung.com › home › java › java string › convert string to int using encapsulation
Convert String to Int Using Encapsulation | Baeldung
January 5, 2024 - In this tutorial, we’ll explore different ways to encapsulate a method to convert String input to an int in Java while handling exceptions elegantly.
🌐
FavTutor
favtutor.com › blogs › int-to-string-java
Convert Int to String in Java (5 Ways) | FavTutor
July 19, 2026 - No casting - (String) number does not compile; int to String always goes through a method. The same conversion in the other direction, and in other languages, follows similar patterns. To compare, read our lesson on converting an int to a string in Python.
🌐
Java67
java67.com › 2013 › 03 › how-to-convert-string-to-int-in-java.html
How to convert String to int or Integer data type in Java? Example Tutorial | Java67
In this Java programming tutorial, we will see all three ways to convert String to an int value in Java. This Java program converts both positive and negative numeric String to int in Java. As I said, it uses three methods. The first example uses the constructor of the Integer class to create an Integer from String, later the Integer is converted to int using autoboxing in Java.
🌐
freeCodeCamp
freecodecamp.org › news › java-string-to-int-how-to-convert-a-string-to-an-integer
Java String to Int – How to Convert a String to an Integer
November 23, 2020 - This leads us to the question – how can we convert a string to an integer? In Java, we can use Integer.valueOf() and Integer.parseInt() to convert a string to an integer.
🌐
CodeGym
codegym.cc › java blog › java numbers › how to convert string to int in java
How to convert String to int in Java
Here is a couple of ways to convert string to int, Java lets do it using parseInt() and valueOf() methods.
Published: December 23, 2024
🌐
SitePoint
sitepoint.com › general web dev
String to integer conversion in Java - General Web Dev - SitePoint Forums | Web Development & Design Community
July 8, 2022 - For one of my project I have to convert String to integer. How to convert “16:45:20” string to integer in java. I am using int time = Integer.parseInt(string) It’s giving me a NumberFormatException error. How to solve…
🌐
Software Testing Help
softwaretestinghelp.com › home › java › how to convert java string to int – tutorial with examples
How To Convert Java String To Int - Tutorial With Examples
April 1, 2025 - For this, we first need to convert these strings to numbers and then perform an arithmetic operation on these numbers. In order to convert Java String to int, we can use the following methods provided by the Java Integer class.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › converting.html
Converting Between Numbers and Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
Sometimes you need to convert a number to a string because you need to operate on the value in its string form. There are several easy ways to convert a number to a string: int i; // Concatenate "i" with an empty string; conversion is handled for you.
🌐
Sentry
sentry.io › sentry answers › java › how do i convert a string to an int in java?
How do I convert a String to an int in Java? | Sentry
class Main { public static void main(String[] args) { String validString = "123"; String invalidString = "123x"; int number; try { number = Integer.parseInt(validString); System.out.println("Converted integer: " + number); number = Integer.parseInt(invalidString); System.out.println("Converted integer: " + number); } catch (NumberFormatException e) { System.out.println("Invalid integer input"); } } } Modern Java versions running on modern systems are very efficient and there should be practically no performance difference between using primitive int and declaring new Integer objects, so you should consider convenience over performance in nearly all cases.
🌐
LeetCode
leetcode.com › problems › string-to-integer-atoi
String to Integer (atoi) - LeetCode
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.