eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

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 – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

As we know, converting a numeric String to an int or Integer is a very common operation in Java.

In this tutorial, we’ll go through two very popular static methods, parseInt() and valueOf() of the  java.lang.Integer class which help us do this conversion. Moreover, we’ll also understand a few differences between these two methods using simple examples.

2. The parseInt() Method

The class java.lang.Integer provides three variants of the parseInt() method. Let’s look at each of them.

2.1. Convert String to Integer

The first variant of parseInt() accepts a String as a parameter and returns the primitive data type int. It throws NumberFormatException when it cannot convert the String to an integer.

Let’s look at its signature:

public static int parseInt(String s) throws NumberFormatException

Now, we’ll see a few examples where we pass signed/unsigned numeric strings as parameters to it to understand how parsing from string to integer happens:

@Test
public void whenValidNumericStringIsPassed_thenShouldConvertToPrimitiveInt() {
    assertEquals(11, Integer.parseInt("11")); 
    assertEquals(11, Integer.parseInt("+11")); 
    assertEquals(-11, Integer.parseInt("-11"));
}

2.2. Specifying Radix

The second variant of the parseInt() method accepts a String and an int as parameters and returns the primitive data type int. Just like the first variant we saw, it also throws NumberFormatException when it cannot convert the String to an integer:

public static int parseInt(String s, int radix) throws NumberFormatException

By default, the parseInt() method assumes that the given String is a base-10 integer. Here, the parameter radix is the radix or base to be used for string to integer conversion.

To understand this better, let’s look at a few examples where we pass a string along with the radix parameter to parseInt():

@Test
public void whenValidNumericStringWithRadixIsPassed_thenShouldConvertToPrimitiveInt() {
    assertEquals(17, Integer.parseInt("11", 16));
    assertEquals(10, Integer.parseInt("A", 16)); 
    assertEquals(7, Integer.parseInt("7", 8));
}

Now, let’s understand how string conversion with radix takes place. For instance, in a number system with radix 13, a string of digits such as 398 denotes the decimal number (with radix/base 10) 632. In other words, in this case, here’s how the calculation happens – 3 × 132 + 9 × 131 + 8 × 130 = 632.

Similarly, in the above example Integer.parseInt(“11”, 16) returned 17 with calculation, 1 × 161 + 1 × 160 = 17.

2.3. Convert Substring to Integer

Lastly, the third variant of the parseInt() method accepts a CharSequence, two integers beginIndex and endIndex of the substring, and another integer radix as parameters. If any invalid string is passed, it throws NumberFormatException:

public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix) throws NumberFormatException

JDK 9 introduced this static method in the Integer class. Now, let’s see it in action:

@Test
public void whenValidNumericStringWithRadixAndSubstringIsPassed_thenShouldConvertToPrimitiveInt() {
    assertEquals(5, Integer.parseInt("100101", 3, 6, 2));
    assertEquals(101, Integer.parseInt("100101", 3, 6, 10));
}

Let’s understand how substring conversion to integer with a given radix takes place. Here, string is “100101”, beginIndex and endIndex are 3 and 6, respectively. Hence, the substring is “101”. For expectedNumber1, radix passed is 2, which means it’s binary. Therefore, substring “101” is converted to integer 5. Further, for expectedNumber2, the radix passed is 10, which means it is decimal. Consequently, substring “101” is converted to integer 101.

Additionally, we can see that Integer.parseInt() throws NumberFormatException when any invalid string is passed:

@Test(expected = NumberFormatException.class)
public void whenInValidNumericStringIsPassed_thenShouldThrowNumberFormatException(){
    int number = Integer.parseInt("abcd");
}

3. The valueOf() Method

Next, let’s take a look at the three variants of the valueOf() method provided by the class java.lang.Integer.

3.1. Convert String to Integer

The first variant of the valueOf() method accepts a String as a parameter and returns the wrapper class Integer. If any non-numeric string is passed, it throws NumberFormatException:

public static Integer valueOf(String s) throws NumberFormatException

Interestingly, it uses parseInt(String s, int radix) in its implementation.

Next, let’s see a few examples of conversion from signed/unsigned numeric string to integer:

@Test
public void whenValidNumericStringIsPassed_thenShouldConvertToInteger() {
    Integer expectedNumber = 11;
    Integer expectedNegativeNumber = -11;
        
    assertEquals(expectedNumber, Integer.valueOf("11"));
    assertEquals(expectedNumber, Integer.valueOf("+11"));
    assertEquals(expectedNegativeNumber, Integer.valueOf("-11"));
}

3.2. Convert int to Integer

The second variant of valueOf() accepts an int as a parameter and returns the wrapper class Integer. Also, it generates a compile-time error if any other data type such as float is passed to it.

Here’s its signature:

public static Integer valueOf(int i)

In addition to int to Integer conversion, this method can also accept a char as a parameter and returns its Unicode value.

To understand this further, let’s see a few examples:

@Test
public void whenNumberIsPassed_thenShouldConvertToInteger() {
    Integer expectedNumber = 11;
    Integer expectedNegativeNumber = -11;
    Integer expectedUnicodeValue = 65;
        
    assertEquals(expectedNumber, Integer.valueOf(11));
    assertEquals(expectedNumber, Integer.valueOf(+11));
    assertEquals(expectedNegativeNumber, Integer.valueOf(-11));
    assertEquals(expectedUnicodeValue, Integer.valueOf('A'));
}

3.3. Specifying Radix

The third variant of valueOf() accepts a String and an int as parameters and returns the wrapper class Integer. Also, like all the other variants we’ve seen, it also throws NumberFormatException when it cannot convert the given string to Integer type:

public static Integer valueOf(String s, int radix) throws NumberFormatException

This method also uses parseInt(String s, int radix) in its implementation.

By default, the valueOf() method assumes that the given String represents a base-10 integer. Additionally, this method accepts another argument to change the default radix.

Let’s parse a few String objects:

@Test
public void whenValidNumericStringWithRadixIsPassed_thenShouldConvertToInetger() {
    Integer expectedNumber1 = 17;
    Integer expectedNumber2 = 10;
    Integer expectedNumber3 = 7;
        
    assertEquals(expectedNumber1, Integer.valueOf("11", 16));
    assertEquals(expectedNumber2, Integer.valueOf("A", 16));
    assertEquals(expectedNumber3, Integer.valueOf("7", 8));
}

4. Differences Between parseInt() and valueOf()

To sum up, here are the main differences between the valueOf() and parseInt() methods:

Integer.valueOf() Integer.parseInt()
It returns an Integer object. It returns a primitive int.
This method accepts String and int as parameters. This method accepts only String as the parameter.
It uses Integer.parseInt() in its method implementation. It doesn’t use any helper method to parse the string as an integer.
This method accepts a character as a parameter and returns its Unicode value. This method will produce an incompatible types error on passing a character as a parameter.

5. Conclusion

In this article, we learnt about the different implementations of parseInt() and valueOf() methods of the java.lang.Integer class. We also looked at the differences between the two methods.

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.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

eBook Jackson – NPI EA – 3 (cat = Jackson)