Java 表达式编译错误
Java expression compilation error
我想知道为什么这段代码无法编译:
int x=-3;
System.out.println(x-----x);
而这段代码是:
int x=-3;
System.out.println(x--- --x);
我认为优先级是pre,然后post递减,然后应该应用减法。
第一个例子就像一个减法
int x=-3;
System.out.println(x-----x);
第二个就像最小化 x -> 一样
x++ => x=x+1
你做的是这样的
x-- => x=x-1
和第二部分:
--x
首先从变量中减去-1
x-----x
从左到右计算:
((x--)--)-x
第一个x--
returns -3,并且不能对值(-3)应用--
运算符,只能对变量(如x)应用.
这就是您收到 Invalid argument to operation ++/--
错误的原因。
当您添加 space - x--- --x
评价为(x--)- (--x)
-3 - -5 = 2
x-----x
被解析为 (x--) -- -x
,这里 --
应用于不是变量的表达式。那是不允许的。
原因如下。解析的第一阶段是对输入流进行标记:由字符组成的输入流被分组为称为标记的块。标记是对 Java 有意义的字符串,例如关键字、运算符或标识符。
标记化是贪心:只要可以将另一个字符添加到标记中,使其仍然是有效标记,就添加该字符。因此,例如 forLoop
被视为单个标识符,而不是关键字 for
后跟标识符 Loop
.
字符串 -
和 --
都是 Java 中的有效标记。因此,当分词器遇到 ---
时,它会读取第一个字符。虽然它知道 -
是一个有效的标记,但它首先查看下一个字符,并决定 --
也是一个有效的标记,因此返回的第一个标记将是 --
,而不是-
.
这直接包含在 Java 语言规范中,§3.2. Lexical Translations
The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would. There is one exception: if lexical translation occurs in a type context (§4.11) and the input stream has two or more consecutive >
characters that are followed by a non->
character, then each >
character must be translated to the token for the numerical comparison operator >
.
The input characters a--b
are tokenized (§3.5) as a
, --
, b
, which is not part of any grammatically correct program, even though the tokenization a
, -
, -
, b
could be part of a grammatically correct program.
因此,由于这不是类型上下文,也不是关于 >
个字符,因此适用最长标记的规则。因此 x-----x
被标记为 x
、--
、--
、-
、x
,就像引用的示例一样。
我想知道为什么这段代码无法编译:
int x=-3;
System.out.println(x-----x);
而这段代码是:
int x=-3;
System.out.println(x--- --x);
我认为优先级是pre,然后post递减,然后应该应用减法。
第一个例子就像一个减法
int x=-3;
System.out.println(x-----x);
第二个就像最小化 x -> 一样
x++ => x=x+1
你做的是这样的
x-- => x=x-1
和第二部分:
--x
首先从变量中减去-1
x-----x
从左到右计算:
((x--)--)-x
第一个x--
returns -3,并且不能对值(-3)应用--
运算符,只能对变量(如x)应用.
这就是您收到 Invalid argument to operation ++/--
错误的原因。
当您添加 space - x--- --x
评价为(x--)- (--x)
-3 - -5 = 2
x-----x
被解析为 (x--) -- -x
,这里 --
应用于不是变量的表达式。那是不允许的。
原因如下。解析的第一阶段是对输入流进行标记:由字符组成的输入流被分组为称为标记的块。标记是对 Java 有意义的字符串,例如关键字、运算符或标识符。
标记化是贪心:只要可以将另一个字符添加到标记中,使其仍然是有效标记,就添加该字符。因此,例如 forLoop
被视为单个标识符,而不是关键字 for
后跟标识符 Loop
.
字符串 -
和 --
都是 Java 中的有效标记。因此,当分词器遇到 ---
时,它会读取第一个字符。虽然它知道 -
是一个有效的标记,但它首先查看下一个字符,并决定 --
也是一个有效的标记,因此返回的第一个标记将是 --
,而不是-
.
这直接包含在 Java 语言规范中,§3.2. Lexical Translations
The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would. There is one exception: if lexical translation occurs in a type context (§4.11) and the input stream has two or more consecutive
>
characters that are followed by a non->
character, then each>
character must be translated to the token for the numerical comparison operator>
.The input characters
a--b
are tokenized (§3.5) asa
,--
,b
, which is not part of any grammatically correct program, even though the tokenizationa
,-
,-
,b
could be part of a grammatically correct program.
因此,由于这不是类型上下文,也不是关于 >
个字符,因此适用最长标记的规则。因此 x-----x
被标记为 x
、--
、--
、-
、x
,就像引用的示例一样。