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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

When we work with integer numbers in Java, sometimes we need to break them into individual digits for various calculations or data manipulation tasks.

In this tutorial, we’ll explore various approaches to splitting an integer number into its constituent digits using Java.

2. Introduction to the Problem

As usual, let’s understand the problem through an example. Let’s say we have an integer number:

int THE_NUMBER = 1230456;

Our goal is to break the number into digits in order:

1, 2, 3, 0, 4, 5, 6

For simplicity, this tutorial will primarily concentrate on positive decimal integers. However, depending on specific needs, we might desire to obtain each digit as an Integer, a char, or a String.

Next, let’s explore various approaches to accommodate these different return types.

3. Splitting to an Integer List

First, let’s break the given integer into a list of Integers. That’s to say, we aim to get this list:

List<Integer> EXPECTED_INT_LIST = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6);

One of the most straightforward approaches is to use a loop to repeatedly divide the number by 10, extracting the reminder in each iteration until the number becomes zero:

      1230456 / 10 = 123045 Reminder: 6
  |-> 123045 / 10 = 12304 Reminder: 5
  |-> 12304 / 10 = 1230 Reminder: 4
  |-> 1230 / 10 = 123 Reminder: 0
  |-> 123 / 10 = 12 Reminder: 3
  |-> 12 / 10 = 1 Reminder: 2
  |-> 1 / 10 = 0 Reminder: 1

However, as the example above shows, this approach generates the digits in the reverse order. To solve this, we can use LinkedList as a stack and push() the reminder to the stack in each step:

int number = THE_NUMBER;
LinkedList<Integer> result = new LinkedList<>();
while (number > 0) {
    result.push(number % 10);
    number /= 10;
}
assertEquals(EXPECTED_INT_LIST, result);

Also, we can use recursion to implement the same idea:

void collectDigits(int num, List<Integer> digitList) {
    if (num / 10 > 0) {
        collectDigits(num / 10, digitList);
    }
    digitList.add(num % 10);
}

Then, we can call the collectDigits() method to get the result:

List<Integer> result = new ArrayList<>();
collectDigits(THE_NUMBER, result);
assertEquals(EXPECTED_INT_LIST, result);

If our Java version is Java 9 or later, we can use String.chars() and IntStream to get the digit list:

String numStr = String.valueOf(THE_NUMBER);
List<Integer> result = numStr.chars().map(Character::getNumericValue).boxed().collect(Collectors.toList());
assertEquals(EXPECTED_INT_LIST, result);

As the code above shows, we first convert the integer to a string. Then, the numStr.chars() method breaks the number string into chars as an IntStream. Next, Character‘s getNumericValue() gets the digits as ints from the char in the IntStream. Finally, we box ints into Integers and collect the result in a list.

4. Splitting to a char Array

Sometimes, we’d like to split the given integer into a char[] array:

char[] EXPECTED_CHAR_ARRAY = new char[] { '1', '2', '3', '0', '4', '5', '6' };

This task is straightforward, as the standard String class offers toCharArray() to break a string into a char[]. What we need to do is simply convert the integer to String and call the toCharArray() method:

String numStr = String.valueOf(THE_NUMBER);
char[] result = numStr.toCharArray();
assertArrayEquals(EXPECTED_CHAR_ARRAY, result);

Of course, if required, we can convert the array to a list quickly.

5. Splitting to a String Array or String List

Finally, let’s see how to split an integer into a String[] array or String list:

String[] EXPECTED_STR_ARRAY = new String[] { "1", "2", "3", "0", "4", "5", "6" };
List<String> EXPECTED_STR_LIST = Lists.newArrayList("1", "2", "3", "0", "4", "5", "6");

Since we call this operation a “split,” the powerful split() method can help us solve the problem:

String numStr = String.valueOf(THE_NUMBER);
String[] result = numStr.split("(?<=.)");
assertArrayEquals(EXPECTED_STR_ARRAY, result);

As we can see, the code looks pretty compact. The key is the regex pattern we used in the split() function. (?<=.) is a positive lookbehind expression. For example, “(?<=X)a” matches every ‘a‘ that is after a ‘X‘ character.

However, we only have (?<=.) in our pattern. This makes the pattern match zero-width. So, split() takes the zero-width “character” after any character as the splitter. It might be easier to understand if we use the ‘#’ character to replace the zero-width “character” in the number string: “1#2#3#0#4#5#6#”. 

Further, split() discards the trailing empty elements by default. Therefore, it produces the desired result.

6. Conclusion

In this article, we’ve explored multiple methods to break an integer into digits. Furthermore, we’ve seen how to obtain the digits in different types, such as Integer, char, and String, through examples.

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 1st 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 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW