1. Overview
1.概述
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.
当我们阅读他人的代码时,有时会发现一些我们从未见过的有趣而新颖的方法。了解这些技巧的作用可以大大提高我们对代码库的理解,并鼓励我们探索自己对 Java 编程的观点。
In this tutorial, we’ll discuss an interesting usage in Java: -->
在本教程中,我们将讨论 Java 中的一种有趣用法:<代码>–></代码
2. Introduction to the Problem
2.问题介绍
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.
乍一看,这段代码非常简单。while 循环重复执行一些逻辑。
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.
但是,如果我们仔细观察 while 语句,那么表达式 i-->0
是做什么的呢?我们以前可能没有见过 -->
操作符的用法。如果我们在 Java 文档中搜索,我们能找到的最接近的运算符是 lambda 表达式中使用的”->”运算符。但显然,示例中的代码与 lambda 表达式毫无关系。
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 ‘-->
‘
3. 理解‘-->
‘
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)
.
首先,( i-->0 )
中的 “-->
“不是 Java 运算符。 实际上,(i-->0)
与 (i-- >0)
或更清晰的格式相同:((i--) > 0)
.
Therefore, it performs two operations: post-decrementing i and the “greater than 0” check.
因此,它会执行两个操作:post-decrementing i 和 “大于 0” 检查。
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:
当我们用数值 6 初始化变量 i 时,它将在循环中产生一系列数字:按照这个序列,i 将转换为-1,从而终止循环:
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:
同样,我们可以在 for-loop 中使用同样的技巧:</em
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!
4.测验时间
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.
每次挑战后,整数列表都会重置,然后在 while 循环中逐步插入数字。我们的目标是在循环执行后确定列表中的数字。
The first challenge is about understanding ++<
.
第一个挑战是如何理解 ++<
。
4.1. Understanding ‘++<
‘
4.1 理解 ‘++<
‘
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:
与 -->
类似, ‘i++<5 后置递增 i 然后比较 i 和 5。由于 i 的初始值是 -1,我们将在结果中得到 0 到 5 的值:
assertEquals(Lists.newArrayList(0, 1, 2, 3, 4, 5), result);
Next, let’s interpret another expression <--
.
接下来,让我们解释另一个表达式 <--
。
4.2. Understanding ‘<--
‘
4.2 理解 ‘<--
‘
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:
这次,如上面的代码所示,后递减操作改为前递减。因此,循环将 9 到 1 填入列表:
assertEquals(Lists.newArrayList(9, 8, 7, 6, 5, 4, 3, 2, 1), result);
Then, let’s look at a similar expression >++
.
然后,让我们看看类似的表达式 >++
。
4.3. Understanding ‘>++
‘
4.3 理解 ‘>++
‘
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:
正如我们在代码片段中看到的,‘6>++n’在每个循环步骤中预先递增 i,然后与 6 进行比较。因此,我们将得到 1 到 5 的结果:
assertEquals(Lists.newArrayList(1, 2, 3, 4, 5), result);
Finally, let’s see a challenge that looks a bit different.
最后,让我们来看看一个看起来有点不同的挑战。
4.4. Understanding ‘>>>=
‘
4.4 理解 ‘>>>=
‘
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.
这次挑战与其他挑战不同。x>>>=1 与前增后减无关。>>> 是无符号右移运算符。
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:
因此,x>>>1
右移一位,结果与 x/2 相同。此外,x >>>=1
执行移位操作,并将结果重新赋值给变量 x。 因此,给定 x=32 时, 结果列表包含 16、8、4 和 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
5.结论
In this article, we discussed what -->
does in Java. Additionally, we engaged in a quiz, exploring a few similar tricks.
在本文中,我们讨论了 -->
在 Java 中的作用。此外,我们还进行了一次测验,探索了一些类似的技巧。
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.
值得注意的是,在使用这些格式时,建议在两个运算符之间插入空格或大括号。这种做法可提高代码的可读性。例如,最好写 i--> 0
或 (i--) > 0
而不是 i-->0.
。
As always, the complete source code for the examples is available over on GitHub.
与往常一样,这些示例的完整源代码可在 GitHub 上获取。