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 read others’ code, sometimes we can see some interesting and novel approaches that we’ve never seen before. Knowing what those tricks do can significantly enhance our comprehension of the codebase and encourage us to explore our viewpoints on Java programming.

In this tutorial, we’ll discuss an interesting usage in Java: -->

2. Introduction to the Problem

As usual, let’s start with an example:

int i = 6;
while ( i-->0 ) { 
    // some processing in loop 
}

At first glance, this code is pretty straightforward. A while loop executes some logic repeatedly.

However, if we take a closer look at the while statement, what does the expression i-->0 do? We may haven’t seen the usage of the --> operator before. If we search in the Java documentation, the closest operator we can find is the ‘->’ operator used in lambda expressions. But apparently, the code in the example has nothing to do with lambda expression.

So next, let’s first figure out what --> does. Then, we’ll have a short quiz for other similar tricks.

For simplicity, we’ll use unit test assertions to present variable values instead of printing them out in this tutorial.

3. Understanding ‘-->‘

First of all, “-->” in ( i-->0 ) isn’t a Java operator. Actually, (i-->0) is the same as (i-- >0) or the more clear format: ((i--) > 0).

Therefore, it performs two operations: post-decrementing i and the “greater than 0” check.

When we initialize the variable i with the value 6, it yields a sequence of numbers within the loop: 5, 4, 3, 2, 1, and 0. Following this sequence, i transitions to -1, causing the loop to terminate:

List<Integer> resultWhile = new ArrayList<>();
int i = 6;
while (i-- > 0) {
    resultWhile.add(i);
}
assertEquals(Lists.newArrayList(5, 4, 3, 2, 1, 0), resultWhile);

Similarly, we can use the same trick in a for-loop:

List<Integer> resultFor = new ArrayList<>();
for (int j = 6; j-- > 0; ) {
    resultFor.add(j);
}
assertEquals(Lists.newArrayList(5, 4, 3, 2, 1, 0), resultFor);

 

Now that we have a clear understanding of what --> does. Let’s explore and experiment with some similar tricks in the upcoming quiz section.

4. Quiz Time!

The quiz has four challenges.

Let’s say we have an integer list:

List<Integer> result = new ArrayList<>();

With each challenge, the integer list is reset, followed by the gradual insertion of numbers within a while loop. Our objective is to determine the numbers in the list after the loop execution.

The first challenge is about understanding ++<.

4.1. Understanding ‘++<‘

int i = -1;
while (i++<5) {
    result.add(i);
}

Similar to -->, ‘i++<5 ‘ post-increments i and then compares i to 5. As i‘s initial value is -1, we’ll have 0 to 5 in the result:

assertEquals(Lists.newArrayList(0, 1, 2, 3, 4, 5), result);

Next, let’s interpret another expression <--.

4.2. Understanding ‘<--‘

result.clear();
int j = 10;
while (0<--j) {
    result.add(j);
}

This time, as the code above shows, the post-decrement operation is changed to pre-decrement. Therefore, the loop fills 9 to 1 to the list:

assertEquals(Lists.newArrayList(9, 8, 7, 6, 5, 4, 3, 2, 1), result);

Then, let’s look at a similar expression >++.

4.3. Understanding  ‘>++‘

result.clear();
int n = 0;
while (6>++n) {
    result.add(n);
}

As we can see in the code snippet, ‘6>++n’ pre-increments i and then compares to 6 in each loop step. Thus, we’ll have 1 to 5 as a result:

assertEquals(Lists.newArrayList(1, 2, 3, 4, 5), result);

Finally, let’s see a challenge that looks a bit different.

4.4. Understanding ‘>>>=‘

result.clear();
int x = 32;
while ((x>>>=1)>1) {
    result.add(x);
}

This challenge is different from others. x>>>=1 has nothing to do with pre-/post-increment or pre-/post-decrement. >>> is the unsigned right-shift operator.

Therefore, x>>>1 right-shifts one place, and the result is the same as x/2. Further, x >>>=1 performs the shift operation and reassigns the result to the variable x. Thus, given x=32, the result list contains 16, 8, 4, and 2:

assertEquals(Lists.newArrayList(16, 8, 4, 2), result);

So far, we’ve gone through the challenges and understood what ‘-->‘ and similar tricks do.

5. Conclusion

In this article, we discussed what --> does in Java. Additionally, we engaged in a quiz, exploring a few similar tricks.

It’s worth noting that when using these formats, it’s advisable to insert a space or braces between the two operators. This practice enhances code readability. For instance, it’s preferable to write i-- > 0 or (i--) > 0 instead of i-->0.

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

2 Comments
Oldest
Newest