在Java中,为什么我不能写i++++或者(i++)++?
当我尝试在/减量中写入后缀/前缀,然后在/减量中写入后缀/前缀时,出现以下错误:操作 ++/-- 的参数无效。
但是,根据 JLS:
PostIncrementExpression:
PostfixExpression ++
所以
PostfixExpression:
Primary
ExpressionName
PostIncrementExpression
PostDecrementExpression
写作:
PostfixExpression ++ ++
应该是可能的......有什么想法吗?
When I try to write a postfix/prefix in/decrement, followed by a post/prefix in/decrement, I get the following error: Invalid argument to operation ++/--.
But, according to JLS:
PostIncrementExpression:
PostfixExpression ++
and
PostfixExpression:
Primary
ExpressionName
PostIncrementExpression
PostDecrementExpression
so writing:
PostfixExpression ++ ++
should be possible... Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
请注意,原始语法缺乏任何语义。这只是语法,并不是每个语法上有效的程序通常都是有效的。例如,语法通常不包含变量必须在使用前声明的要求(可以,但很麻烦)。
后置增量产生一个右值——就像你不能对文字进行后置增量一样,你也不能对
i++
的结果进行后置增量。引自 JLS(第 3 版,第 486 页):
Note that the raw grammar lacks any semantics. It's just syntax, and not every syntactically valid program will generally be valid. For example, the requirement that variables have to be declared before usage is typically not covered by the grammar (you can, but it's cumbersome).
Postfix-increment yields an rvalue – and just as you cannot postfix-increment literals, you cannot postfix-increment the result of
i++
.Quoting from the JLS (3rd ed., page 486):
该错误告诉您答案:
因此,i++ 计算结果为一个值,而运算符需要一个变量。
The error tells you the answer:
So, the i++ evaluates to a value while the operator requires a variable.
您只能将
++
或--
应用于表示可修改位置(左值)的表达式。++
或--
的 RESULT 是来自该位置的值(右值 - 在递增或递减之前或之后),而不是其本身是可修改的位置。因此,您不能说(a++)++
,就像您不能说(a+b)++
一样——没有位置可以修改。You can only apply
++
or--
to an expression that denotes a modifiable location (an lvalue). The RESULT of a++
or--
is the value from the location (an rvalue -- either before or after the increment or decrement), and not itself a modifiable location. So you can't say(a++)++
any more than you can say(a+b)++
-- there's no location to be modified.这个表达式 i = (i++)++; 的问题在于 (i++) 被解析为一个值,而 ++ 运算符的定义表明:
1 . 它将递增指定的变量,并为其放置/返回一个值,无论您使用 Postfix 还是 Prefix。
2. 该运算符需要变量,无论是前缀还是后缀。但这里发生的是 (i++) 返回一个值并将其代替 (i++),然后您将得到 (value)++ 和 (value) 是不是该运算符的预期类型,因为它需要一个变量来代替值。
例如,在 Java 中,如果编译以下代码片段,您将收到如下所示的错误:
public class A{
公共无效测试(){
整数i=0;
我 = (i++)++;
}
编译
输出:
A.java:4: 意外类型
必需:变量
发现:值
我 = (i++)++;
^
1 个错误
The problem with this expression i = (i++)++; is that (i++) gets resolved to a value and the definition of ++ operator says that
1 . it will increment the variable specified and will put/return a value for this whether you use Postfix or Prefix.
2. This operator requires variable whether prefix or postfix. But what's happening here is (i++) is return a value and putting it in place of (i++) and then you are having (value)++ and (value) is not the expected type for this operator as it requires a variable in place of value.
for example in Java if you compile the following code snippet you will get error as is shown after snippet:
public class A{
public void test(){
int i =0;
i = (i++)++;
}
}
Compilation output :
A.java:4: unexpected type
required: variable
found : value
i = (i++)++;
^
1 error
的快捷方式
i++ 基本上是: (i = i+1)
没有任何意义
,并且编写: (i = i+1)++;
。正确的? :)
i++ is basically a shortcut for:
(i = i+1)
And it wouldn't make any sense to write:
(i = i+1)++;
right? :)
这样的操作应该得到什么结果呢?
i++
的结果是i
的当前值(副本),并且i
随后递增(post)。那么您如何想象再次增加i++
的结果呢?如果i
原本是1,那么i++++
之后的值应该是多少,这个运算的结果应该是什么?如果您考虑一下,您可能会意识到正确定义这一点非常。由于 Java 的设计者打算避免 C/C++ 的“未定义”陷阱(而且这种声明的价值充其量是可疑的),因此他们可能决定明确禁止它。
What should be the result of such an operation? The result of
i++
is (a copy of) the current value ofi
, andi
is incremented afterwards (post). So how do you imagine incrementing the result ofi++
once again? Ifi
originally was 1, what should its value be afteri++++
, and what should be the result of this operation?If you think about it, you probably realize it would be very difficult to define this properly. Since the designers of Java intended to avoid the C/C++ "undefined" traps (and since the value of such a statement is dubious at best), they probably decided to explicitly disallow it.
必须记住:增量和增量减量(前缀和后缀)运算符始终与变量一起使用,而不是值
这里我用一个exe来解释这一点:
上面的exe。
Must Remember: Increment & decrement (prefix & postfix) operator always work with variable not value
Here i am explain this with one exe :
above exe.
您想通过 (i++)++ 实现什么?
增加两倍!!
使用循环
循环内增量:)
What you trying to achieve by (i++)++ ?
increment it twice!!
Use Looping
Increment inside a loop :)
在i++中,++是后缀运算符,因此它返回i的值,然后将其递增。你想要 (i++)++ 做什么?因为 (i++ == i ) 它应该返回 i+1 吗?如果不是,那么 i++=i 但 (i++)++ != i) 是否会很奇怪?考虑以下表达式:
你考虑过 i+=2 吗?
In i++, ++ is a postfix operator, so it returns the value of i and then increments it. What would you want (i++)++ to do? Should it return i+1 since (i++ == i )? If not wouldn't that be strange that i++=i but (i++)++ != i) consider the following expressions:
Have you considered i+=2?
为什么不直接使用简写增量运算符呢?
那里。
Why don't you just use the shorthand increment operator?
There.