无效的Java语句

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

Invalid Java statement

问题

我想知道为什么以下内容在Java中是无效的。Java编译器指出这不是一个有效的语句。

1+1;

我知道以下内容是有效的。

int i = 1+1;

请解释为什么第二个有效而第一个无效。提前谢谢。

英文:

I want to know why the following is invalid in Java. Java compiler says that it is not a valid statement.

1+1;

I know the following works.

int i = 1+1;

Please explain why the second one is valid while the first is not. Thanks in advance.

答案1

得分: 0

因为你对1+1没有做任何操作。那不是一个语句,而是一个返回值的表达式,应该存储在某个地方,就像你给出的第二个例子中那样。如果你的语句没有任何效果,它们将被排除在语言语法之外。

英文:

Because you are doing nothing with 1+1. That is not a statement, it's an expression that returns a value that should be stored somewhere, like in the second example you give. If your statements have no effect, they are excluded from the language grammar.

答案2

得分: -2

Java的语法需要变量声明如下:

Class name = value;

你不能在没有变量定义的情况下创建一个值,也不能在没有名称和类的情况下创建一个变量。

英文:

The Java syntax needs the variable to be declared like the following

Class name = value;

You can't create a value without a variable definition and can't create a variable without a name and a class.

huangapple
  • 本文由 发表于 2020年8月25日 17:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63576331.html
匿名

发表评论

匿名网友

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

确定