英文:
What's the meaning of assigning one variable with 2 values in Java
问题
我在Java方面还很新,今天看到了这段代码,但不明白它的含义。它将一个变量赋值为2个值,那么变量'actual'的值是什么?
Token actual = new Token(MODULE, 0, 0, ""), expected;
英文:
I am new in Java, I saw this code today and don't get what it means. It is assigning a variable with 2 values, so what is the value of this variable 'actual'?
Token actual=new Token(MODULE, 0, 0, ""), expected;
答案1
得分: 4
它创建了两个变量:actual,被赋值为new Token(MODULE, 0, 0, "");以及expected,未被赋值。
英文:
It creates two variables: actual, which is assigned new Token(MODULE, 0, 0, ""); and expected, which is left unassigned.
答案2
得分: 3
变量声明的基本语法实际上是:
<类型> <名称> [ = <值> ] [ , <名称> [ = <值> ] ]...
例如,以下都是有效的:
int x;             // 声明x,不赋值
int x = 5;         // 声明x,并初始化为5
int x, y;          // 声明x和y,不赋值
int x = 5, y = 7;  // 声明x和y,并分别初始化为5和7
int x, y = 7;      // 声明x和y,初始化y,不赋值x
int x = 5, y;      // 声明x和y,初始化x,不赋值y
问题中的代码声明了两个变量,分别命名为actual和expected,初始化了actual,并未赋值expected。
英文:
The base syntax of a variable declaration is actually:
<type> <name> [ = <value> ] [ , <name> [ = <value> ] ]...
E.g. these are all valid:
int x;             // declares x, leaving it unassigned
int x = 5;         // declares x, initializing it
int x, y;          // declares x and y, leaving both unassigned
int x = 5, y = 7;  // declares x and y, initializing both
int x, y = 7;      // declares x and y, initializing y, leaving x unassigned
int x = 5, y;      // declares x and y, initializing x, leaving y unassigned
The code in the question declares 2 variables named actual and expected, initializes actual, and leaves expected unassigned.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论