Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this short tutorial, we’ll show different ways to convert from String to Integer in Groovy.

2.  Casting with as

The first method that we can use for the conversion is the as keyword, which is the same as calling the class’s asType() method:

def "givenString_whenUsingAsInteger_thenConvertToInteger"() {
    given:
    def invalidString = "123a"
    Integer integerNum = STRING_NUM as Integer

    when:
    def intNum = invalidString?.isInteger() ? invalidString as Integer : null

    then:
    intNum == null
    integerNum == EXPECTED_INT
}

Like the above, we can use as int:

def "givenString_whenUsingAsInt_thenConvertToInt"() {
    given:
    int intNum = STRING_NUM as int

    expect:
    intNum == EXPECTED_INT
}

3. toInteger

Another method is from the Groovy JDK extension for java.lang.CharSequence:

def "givenString_whenUsingToInteger_thenConvertToInteger"() {
    given:
    int intNum = STRING_NUM.toInteger()

    expect:
    intNum == EXPECTED_INT
}

4. Integer#parseInt

A third way is to use Java’s static method Integer.parseInt():

def "givenString_whenUsingParseInt_thenConvertToInteger"() {
    given:
    int intNum = Integer.parseInt(STRING_NUM)

    expect:
    intNum == EXPECTED_INT
}

5. Integer#intValue

An alternative method is to create a new Integer object and call its intValue method:

def "givenString_whenUsingIntValue_thenConvertToInteger"() {
    given:
    int intNum = Integer.valueOf(STRING_NUM).intValue()

    expect:
    intNum == EXPECTED_INT
}

Or, in this case, we can also use just new Integer(stringNum):

def "givenString_whenUsingNewInteger_thenConvertToInteger"() {
    given:
    Integer intNum = Integer.valueOf(STRING_NUM)

    expect:
    intNum == EXPECTED_INT
}

6. Integer#valueOf

Similar to Integer.parseInt(), we can also use Java’s static method Integer#valueOf:

def "givenString_whenUsingValueOf_thenConvertToInteger"() {
    given:
    int intNum = Integer.valueOf(STRING_NUM)

    expect:
    intNum == EXPECTED_INT
}

7. DecimalFormat

And for our last method, we can apply Java’s DecimalFormat class:

def "givenString_whenUsingDecimalFormat_thenConvertToInteger"() {
    given:
    DecimalFormat decimalFormat = new DecimalFormat("#")

    when:
    int intNum = decimalFormat.parse(STRING_NUM).intValue()

    then:
    intNum == EXPECTED_INT
}

8. Exception Handling

So, if conversion fails, like if there are non-numeric characters, a NumberFormatException will be thrown. Additionally, in the case where String is null, NullPointerException will be thrown:

def "givenInvalidString_whenUsingAs_thenThrowNumberFormatException"() {
    given:
    def invalidString = "123a"

    when:
    invalidString as Integer

    then:
    thrown(NumberFormatException)
}
def "givenNullString_whenUsingToInteger_thenThrowNullPointerException"() {
    given:
    def invalidString = null

    when:
    invalidString.toInteger()

    then:
    thrown(NullPointerException)
}

To prevent this from happening, we can use the isInteger method:

def "givenString_whenUsingIsInteger_thenCheckIfCorrectValue"() {
    given:
    def invalidString = "123a"
    def validString = "123"

    when:
    def invalidNum = invalidString?.isInteger() ? invalidString as Integer : false
    def correctNum = validString?.isInteger() ? validString as Integer : false

    then:
    !invalidNum
    correctNum == 123
}

9. Summary

In this short article, we’ve shown some effective ways to switch from String to Integer objects in Groovy.

When it comes to choosing the best method to convert an object type, all of the above are equally good. The most important thing is to avoid errors by first checking if the value of the String in our application can be non-numeric, empty, or null.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW