Course – LS – All

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

>> CHECK OUT THE COURSE

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.

As always, the complete source code for the examples is available over on GitHub.

Course – LS – All

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

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.