在Java中将一个变量赋予两个值的含义是什么?

huangapple go评论73阅读模式
英文:

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

问题中的代码声明了两个变量,分别命名为actualexpected,初始化了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.

huangapple
  • 本文由 发表于 2020年9月15日 23:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63905080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定