在Java中,为什么我不能写i++++或者(i++)++?

发布于 2024-10-11 15:07:44 字数 427 浏览 11 评论 0原文

当我尝试在/减量中写入后缀/前缀,然后在/减量中写入后缀/前缀时,出现以下错误:操作 ++/-- 的参数无效

但是,根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

眼泪淡了忧伤 2024-10-18 15:07:45

请注意,原始语法缺乏任何语义。这只是语法,并不是每个语法上有效的程序通常都是有效的。例如,语法通常不包含变量必须在使用前声明的要求(可以,但很麻烦)。

后置增量产生一个右值——就像你不能对文字进行后置增量一样,你也不能对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):

The result of the postfix increment expression is not a variable, but a value.

英雄似剑 2024-10-18 15:07:45

该错误告诉您答案:

unexpected type
required: variable
found   : value
        (i++)++;

因此,i++ 计算结果为一个值,而运算符需要一个变量。

The error tells you the answer:

unexpected type
required: variable
found   : value
        (i++)++;

So, the i++ evaluates to a value while the operator requires a variable.

诗化ㄋ丶相逢 2024-10-18 15:07:45

您只能将 ++-- 应用于表示可修改位置(左值)的表达式。 ++-- 的 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.

爱本泡沫多脆弱 2024-10-18 15:07:45

这个表达式 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

尹雨沫 2024-10-18 15:07:45

的快捷方式

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? :)

流云如水 2024-10-18 15:07:45

这样的操作应该得到什么结果呢? 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 of i, and i is incremented afterwards (post). So how do you imagine incrementing the result of i++ once again? If i originally was 1, what should its value be after i++++, 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.

找回味觉 2024-10-18 15:07:45

必须记住:增量和增量减量(前缀和后缀)运算符始终与变量一起使用,而不是

这里我用一个exe来解释这一点:

int i = 0;

int j = (i++)++;

上面的exe。

i的值初始化为0(零)

(i++) '++' 中与 i 一起工作,i 是变量 &执行操作后返回(1)

现在在(1)++'++'中使用(1),它是值而不是变量,与java的规则相反,这就是为什么编译时错误生成Invalid argument to操作++/--

Must Remember: Increment & decrement (prefix & postfix) operator always work with variable not value

Here i am explain this with one exe :

int i = 0;

int j = (i++)++;

above exe.

initialize value of i with 0(zero)

in (i++) '++' work with i which is variable & after performing operation return (1)

now in (1)++ '++' work with (1) which is value not variable that is oppose to rules of java that's why compile time error generate Invalid argument to operation ++/--

孤城病女 2024-10-18 15:07:45

您想通过 (i++)++ 实现什么?

增加两倍!!

使用循环

循环内增量:)

What you trying to achieve by (i++)++ ?

increment it twice!!

Use Looping

Increment inside a loop :)

裂开嘴轻声笑有多痛 2024-10-18 15:07:45

在i++中,++是后缀运算符,因此它返回i的值,然后将其递增。你想要 (i++)++ 做什么?因为 (i++ == i ) 它应该返回 i+1 吗?如果不是,那么 i++=i 但 (i++)++ != i) 是否会很奇怪?考虑以下表达式:

i++  ==  i;   // true
j = i++  // j gets the value of i
j++ == i //  still true since j== i

(i++) ++ ==  j ++    //  what should be the answer here?

你考虑过 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:

i++  ==  i;   // true
j = i++  // j gets the value of i
j++ == i //  still true since j== i

(i++) ++ ==  j ++    //  what should be the answer here?

Have you considered i+=2?

爱你不解释 2024-10-18 15:07:45

为什么不直接使用简写增量运算符呢?

i+=2

那里。

Why don't you just use the shorthand increment operator?

i+=2

There.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文