Java 间距:赋值时出错
Java spacing: Getting error while assigning values
我正在学习 Java 深入的基础知识。我有一个疑问。
示例代码:
public static void main(String arg)
{
int i=1++1; /* this is not working*/
int i=1+ +1; /* this is working*/
}
请解释这是怎么回事...
解释如下:
int i=1++1; /* this is not working*/
这不起作用,因为解析器将 ++
解释为递增运算符,您不能将其应用于常量。它必须应用于整数或长型变量。
我建议您更仔细地阅读有关 Java operators 的内容。
int i=1+ +1; /* this is working*/
这是可行的,因为解析器将其解释为 'add +1 to 1',即 2。
无论哪种情况,这些都是难以阅读和解释代码的示例。您应该学习一种间距适当的清晰样式并坚持下去。
在编译期间,Java 源代码(在 unicode 翻译之后)经历了 Java Language Specification, chapter 3 中描述的词法翻译。词汇翻译用于将程序拆分为所谓的“输入元素”序列。因此 int i=1++1;
行被拆分为以下元素:
int
: token/keyword
<space>
: 空格
i
: token/identifier
1
: token/literal
++
: token/operator
1
: token/literal
;
: token/separator
此时编译器对这些标记的含义知之甚少。但是,它不能将 ++
运算符拆分为两个单独的运算符,如 JLS 3.2:
中所述
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.
Thus, 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.
强调我的。
JLS 3.5 也记录了使用空格分隔标记的可能用法:
White space (§3.6) and comments (§3.7) can serve to separate tokens that, if adjacent, might be tokenized in another manner. For example, the ASCII characters -
and =
in the input can form the operator token -=
(§3.12) only if there is no intervening white space or comment.
这解释了为什么 1+ +1
以不同的方式解释。您还可以使用注释:1+/**/+1
.
第一种情况:
int i=1++1; /* this is not working*/
- 1 被解释为数字
- ++被解释为一元递增运算符
- 1 被解释为数字
所以你在编译时有这样的错误:
Multiple markers at this line
- Invalid argument to operation ++/--
- The left-hand side of an assignment must be a variable
- Syntax error on token "++", invalid AssignmentOperator
第二种情况:
int i=1+ +1; /* this is working*/
+ 和 + 之间的 space 决定了不同的分词
- 1 被解释为数字
- +被解释为加法运算符
- +被解释为加法运算符
- 1 被解释为数字
我正在学习 Java 深入的基础知识。我有一个疑问。
示例代码:
public static void main(String arg)
{
int i=1++1; /* this is not working*/
int i=1+ +1; /* this is working*/
}
请解释这是怎么回事...
解释如下:
int i=1++1; /* this is not working*/
这不起作用,因为解析器将 ++
解释为递增运算符,您不能将其应用于常量。它必须应用于整数或长型变量。
我建议您更仔细地阅读有关 Java operators 的内容。
int i=1+ +1; /* this is working*/
这是可行的,因为解析器将其解释为 'add +1 to 1',即 2。
无论哪种情况,这些都是难以阅读和解释代码的示例。您应该学习一种间距适当的清晰样式并坚持下去。
在编译期间,Java 源代码(在 unicode 翻译之后)经历了 Java Language Specification, chapter 3 中描述的词法翻译。词汇翻译用于将程序拆分为所谓的“输入元素”序列。因此 int i=1++1;
行被拆分为以下元素:
int
: token/keyword<space>
: 空格i
: token/identifier1
: token/literal++
: token/operator1
: token/literal;
: token/separator
此时编译器对这些标记的含义知之甚少。但是,它不能将 ++
运算符拆分为两个单独的运算符,如 JLS 3.2:
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.
Thus, 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.
强调我的。
JLS 3.5 也记录了使用空格分隔标记的可能用法:
White space (§3.6) and comments (§3.7) can serve to separate tokens that, if adjacent, might be tokenized in another manner. For example, the ASCII characters
-
and=
in the input can form the operator token-=
(§3.12) only if there is no intervening white space or comment.
这解释了为什么 1+ +1
以不同的方式解释。您还可以使用注释:1+/**/+1
.
第一种情况:
int i=1++1; /* this is not working*/
- 1 被解释为数字
- ++被解释为一元递增运算符
- 1 被解释为数字
所以你在编译时有这样的错误:
Multiple markers at this line
- Invalid argument to operation ++/--
- The left-hand side of an assignment must be a variable
- Syntax error on token "++", invalid AssignmentOperator
第二种情况:
int i=1+ +1; /* this is working*/
+ 和 + 之间的 space 决定了不同的分词
- 1 被解释为数字
- +被解释为加法运算符
- +被解释为加法运算符
- 1 被解释为数字