在Java中,为什么我不能编写i ++++或(i ++)++? [英] In Java, why can't I write i++++ or (i++)++?

查看:258
本文介绍了在Java中,为什么我不能编写i ++++或(i ++)++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在/decrement中写一个后缀/前缀,然后在/decrement中写一个后缀/前缀时,出现以下错误:无效的参数++/-./p>

但是,根据JLS:

PostIncrementExpression:
        PostfixExpression ++

PostfixExpression:
        Primary
        ExpressionName
        PostIncrementExpression
        PostDecrementExpression

如此写作:

PostfixExpression ++ ++

应该有可能...有什么想法吗?

解决方案

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

后缀增加会产生一个右值-就像不能后缀增加文字一样,也不能后缀增加i++的结果.

从JLS报价(第3 rd 版,第486页):

后缀增量表达式的结果不是变量,而是值.

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?

解决方案

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.

这篇关于在Java中,为什么我不能编写i ++++或(i ++)++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆