How to Convert String to int in Java

In this Java tutorial, we will learn how to convert a String into an int value using both traditional Java and Java 8.

For example:

String : "100"
int    : 100

1. Traditional Java Program

The Integer.parseInt() method is the simplest way to convert a String into an int.

public class StringToInt {

    public static void main(String[] args) {
        String str = "100";
        int number = Integer.parseInt(str);
        System.out.println("String: " + str);
        System.out.println("Integer: " + number);
    }
}

Output

String: 100
Integer: 100

Explanation

The following statement converts the String "100" into the integer 100:

int number = Integer.parseInt(str);

Here:

  • str is a String.
  • Integer.parseInt() converts the String into a primitive int.
  • number stores the converted integer value.

For example:

String str = "250";

int number = Integer.parseInt(str);

Now number contains the integer value 250.


2. Java 8 Program

Java 8 does not introduce a special new method for converting a String to an int. The recommended method is still Integer.parseInt().

However, Java 8 also allows us to use the Stream API when we need to convert multiple String values.

import java.util.Arrays;

public class StringToIntJava8 {

    public static void main(String[] args) {
        String[] strArray = {"10", "20", "30", "40"};
        int[] numbers = Arrays.stream(strArray)
                .mapToInt(Integer::parseInt)
                .toArray();
        System.out.println("Integer Array: "
                + Arrays.toString(numbers));
    }
}

Output

Integer Array: [10, 20, 30, 40]

Explanation of Java 8 Program

First, the String array is converted into a Stream:

Arrays.stream(strArray)

Then, mapToInt() converts each String into an integer:

.mapToInt(Integer::parseInt)

Here, Integer::parseInt is a Java 8 method reference.

Finally, toArray() converts the values into an int array:

.toArray();

3. Converting a Single String in Java 8

If you only need to convert one String to an integer, simply use:

String str = "100";

int number = Integer.parseInt(str);

This works perfectly in Java 8.

You do not need to use Streams for a single String conversion.


Difference Between Traditional Java and Java 8

FeatureTraditional JavaJava 8
Single String conversionInteger.parseInt()Integer.parseInt()
Multiple String conversionLoopStream API
LambdaNoOptional
Method referenceNoInteger::parseInt
Easy for beginnersVery easyModerate
Java VersionJava 5+Java 8+

Important Note

If the String does not contain a valid integer, Integer.parseInt() throws a NumberFormatException.

For example:

String str = "ABC";

int number = Integer.parseInt(str);

This will produce:

NumberFormatException

Therefore, make sure the String contains a valid numeric value before converting it.

Conclusion

Converting a String to an int is very easy in Java. The Integer.parseInt() method is the standard approach and works in Java 8 as well. For converting multiple String values, Java 8 Streams and method references can make the code concise and readable.

Backend developer working with Java, Spring Boot, Microservices, NoSQL, and AWS. I love sharing knowledge, practical tips, and clean code practices to help others build scalable applications.

Leave a Reply

Your email address will not be published. Required fields are marked *