Java:不是声明 [英] Java: Not a statement

查看:100
本文介绍了Java:不是声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个关于语言理论的问题,而不是其他问题。为什么第一个声明属于主要法律,而第二个声明不是?他们不评价是同一个东西吗?

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?

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


推荐答案

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

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.

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

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 can have side effects, so it is syntactically valid.

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

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

return;
foo();    // unreachable statement

C和C ++相对自由放任。写任何你想要的东西;他们没有时间照看你。

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

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.

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

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!

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

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.

这篇关于Java:不是声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆