Java:不是声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16599183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:25:43  来源:igfitidea点击:

Java: Not a statement

javasemantics

提问by Skyler

I suppose this is more a question about language theory than anything else. Why is the first statement in main legal, when the second is not? Don't they evaluate to be the same thing?

我想这更像是一个关于语言理论的问题。为什么 main 中的第一个语句是合法的,而第二个语句则不是?他们不是评估为同一件事吗?

public class Main {
        public static void main(String[] args) {
                foo();
                0;
        }
        public static int foo(){
                return 0;
        }
}

回答by John Kugelman

Java restricts the types of expressions that are allowed in so-called "expression statements". Only meaningful expressions that have potential side effects are allowed. It disallows semantically meaningless statements like 0;or a + b;. They're simply excluded from the language grammar.

Java 限制了所谓的“表达式语句”中允许的表达式类型。只允许具有潜在副作用的有意义的表达式。它不允许语义上无意义的语句,如0;or a + b;。它们只是被排除在语言语法之外。

A function call like foo()can, and usually does, have side effects, so it is not a meaningless statement. The compiler doesn't deeply inspect the body of foo()to check whether it actually does anything. Calling a function canhave side effects, so it is syntactically valid.

foo()can这样的函数调用,通常确实有副作用,所以它不是一个毫无意义的语句。编译器不会深入检查 的主体foo()以检查它是否实际执行任何操作。调用函数可能会产生副作用,因此它在语法上是有效的。

This reflects a philosophical difference between C/C++ and Java. Java prohibits various constructs which result in dead or meaningless code.

这反映了 C/C++ 和 Java 之间的哲学差异。Java 禁止导致死代码或无意义代码的各种构造。

return;
foo();    // unreachable statement

C and C++ are relatively laissez faire about it all. Write whatever you want; they don't have time to babysit you.

C 和 C++ 对这一切都相对放任自流。随便写;他们没有时间照顾你。



Quoting from the Java Language Specification, §14.8 Expression Statements:

引自Java 语言规范,§14.8 表达式语句

Certain kinds of expressions may be used as statements by following them with semicolons.

ExpressionStatement:
    StatementExpression ;

StatementExpression:
    Assignment
    PreIncrementExpression
    PreDecrementExpression
    PostIncrementExpression
    PostDecrementExpression
    MethodInvocation
    ClassInstanceCreationExpression

An expression statement is executed by evaluating the expression; if the expression has a value, the value is discarded.

Execution of the expression statement completes normally if and only if evaluation of the expression completes normally.

Unlike C and C++, the Java programming language allows only certain forms of expressions to be used as expression statements. Note that the Java programming language does not allow a "cast to void" - void is not a type - so the traditional C trick of writing an expression statement such as:

(void)... ;  // incorrect!

does not work. On the other hand, the Java programming language allows all the most useful kinds of expressions in expressions statements, and it does not require a method invocation used as an expression statement to invoke a void method, so such a trick is almost never needed. If a trick is needed, either an assignment statement (§15.26) or a local variable declaration statement (§14.4) can be used instead.

某些类型的表达式可以通过在它们后面加上分号来用作语句。

ExpressionStatement:
    StatementExpression ;

StatementExpression:
    Assignment
    PreIncrementExpression
    PreDecrementExpression
    PostIncrementExpression
    PostDecrementExpression
    MethodInvocation
    ClassInstanceCreationExpression

通过对表达式求值来执行表达式语句;如果表达式有值,则丢弃该值。

当且仅当表达式的计算正常完成时,表达式语句的执行才能正常完成。

与 C 和 C++ 不同,Java 编程语言只允许将某些形式的表达式用作表达式语句。请注意,Java 编程语言不允许“强制转换为 void”——void 不是一种类型——所以编写表达式语句的传统 C 技巧,例如:

(void)... ;  // incorrect!

不起作用。另一方面,Java 编程语言允许在表达式语句中使用所有最有用的表达式,并且它不需要用作表达式语句的方法调用来调用 void 方法,因此几乎不需要这样的技巧。如果需要技巧,可以使用赋值语句(第 15.26 节)或局部变量声明语句(第 14.4 节)来代替。

回答by SaravanaKumar KKB

In the first statement you are actually calling a function and second statement doesn't give any value. Incase you want to process the return value, you need to call a variable for return type

Eg: 
public class Main {
        public static void main(String[] args) {
               int n = foo();

               //do whatever you want with return 

        }
        public static int foo(){
                return 0;
        }
}