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
🌐
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
🌐
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
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. Although initializing new Integer objects will take up some extra memory, this will be trivial unless you are keeping vast amounts in memory simultaneously. ... If you need to store the result in a database, then Integer is nullable which might be useful.
🌐
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 - First we’ll ini­tial­ize a string that contains the value “7312” and nothing else. Then we’ll convert that Java string to an int using parseInt(), and finally we’ll output the result using the Java command System.out.println.
🌐
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 - Since we're trying to convert a non-numeric number to numeric, the exception will be a NumberFormatException in the Java compiler. The code to wrap the pareseInt() function in a try and catch block is displayed here: String myString = newString("hello"); try { int makeNumber = Integer.parseInt(myString); System.out.println(makeNumber); } catch (NumberFormatexception e) { System.out.println("That wasn't a number!"); }
🌐
GeeksforGeeks
geeksforgeeks.org › java › different-ways-for-integer-to-string-conversions-in-java
Java Convert int to String | How to Convert an Integer into a String - GeeksforGeeks
April 9, 2025 - ... // Java Program to Illustrate Integer to String Conversions // Using Concatenation with Empty String class Geeks { // Main driver method public static void main(String args[]) { // Custom integer values int a = 1234; int b = -1234; // ...
🌐
Programiz
programiz.com › java-programming › examples › string-to-int-conversion
Java Program to convert string type variables into int
class Main { public static void main(String[] args) { // create a string variable String str1 = "Programiz"; // convert string to int // using parseInt() int num1 = Integer.parseInt(str1); // print int values System.out.println(num1); // throws NumberFormatException } } We can also convert ...
Find elsewhere
🌐
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); } }
🌐
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)
For example, in the ValueOfDemo program, we could use: float a = Float.parseFloat(args[0]); float b = Float.parseFloat(args[1]); 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 ...
🌐
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 - In Java, we can use Integer.valueOf() and Integer.parseInt() to convert a string to an integer. This method returns the string as a primitive type int. If the string does not contain a valid integer then it will throw a NumberFormatException.
🌐
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 - Each method is Explained with a brief Description, Syntax and an Example: This tutorial will help you to understand how to manipulate Strings in Java with ease using the inbuilt methods. String manipulation involves tasks like concatenating… ... In this tutorial we will explore the different methods to convert Integer to String in Java along with interesting programming examples: We will be covering the use of the following methods provided by the different Java classes to convert Int to String in Java: String concatenation String.valueOf() String.format() Integer.toString() Integer.String(int)…
🌐
CodeGym
codegym.cc › java blog › java numbers › how to convert string to int in java
How to convert String to int in Java
In this article we are going to discuss converting String to int (primitive type) and Object type (wrapper) Integer. There are many ways to do it in Java.
Published   December 23, 2024
🌐
Java67
java67.com › 2015 › 08 › 2-ways-to-parse-string-to-int-in-java.html
2 ways to parse String to int in Java - Example Tutorial | Java67
Here is our Java program to convert String to int in Java. This example shows how to use both Integer.parseInt() and Integer.valueOf() method to parse a numeric String to an int primitive in Java.
🌐
DigitalOcean
digitalocean.com › community › questions › how-to-convert-a-string-to-an-integer-in-java
How to convert a string to an integer in Java? | DigitalOcean
September 24, 2021 - In order to set a string variable, we have to use the keyword String, and after it, we set our variables name, and then we put in its value wrapped in quotations marks(" ") or single quotation marks('') like this: ... Integers are whole numbers.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-convert-string-to-int-in-java
Java String to int Conversion
June 10, 2024 - I have covered this method in detail here: Integer.parseInt() Method · String number = "123"; int result = Integer.parseInt(number); System.out.println(result); // Output: 123 · The Integer.valueOf() method converts a String to an Integer object, which can then be unboxed to a primitive int.
🌐
Vultr Docs
docs.vultr.com › java › examples › convert-string-type-variables-into-int
Java Program to convert string type variables into int | Vultr Docs
December 16, 2024 - // parseInt example int num1 = Integer.parseInt("789"); // valueOf example Integer num2 = Integer.valueOf("789"); System.out.println(num1); System.out.println(num2); Explain Code · Although these methods serve slightly different purposes, they ...
🌐
Educative
educative.io › answers › how-to-convert-an-integer-to-a-string-in-java
How to convert an integer to a string in Java
The first argument is any string containing %d. The second argument is the integer you want to convert. This method will replace %d with your integer.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-convert-a-string-to-an-integer-in-java
String to Int in Java – How to Convert a String to an Integer
November 3, 2022 - We saw how to convert a string to an integer in Java using two methods of the Integer class — parseInt() and valueOf().
🌐
Edureka
edureka.co › blog › string-to-int-in-java
How to Convert a String to Int in Java: Methods and Examples | Edureka
October 1, 2019 - Java Tutorial For Beginners – Java Programming Made Easy! ... There are many methods to convert a string to integer in Java. The method most commonly used is the pareseInt() method. There are multiple methods to convert string to an integer.