Java程序中的一行和语句一样吗?
我是一个 Java 菜鸟。我只使用了几天,我仍在努力弄清楚这一切。在程序中,行和语句是一样的吗?
I'm a Java noob. I've only used it for a few days and I'm still trying to figure it all out. In a program, is a line the same thing as a statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,我可以写:
这是一行,两个陈述。
No. I can write:
That's one line, and two statements.
不会。Java 编译器在编译程序时不会考虑行、间距或其他格式问题。它只是想看到每个语句末尾的
;
。此行可以正常工作:但是,执行此类操作可能(并且很可能会)导致源代码的可读性问题。因此,不建议这样做。
单个语句也可以跨越多行:
No. The Java compiler doesn't look at lines, spacing, or other formatting issues when compiling a program. It just wants to see the
;
at the end of each statement. This line would work just fine:However, doing things like this can--and most likely will--cause readability issues with the source code. For this reason, it isn't recommended.
It is also possible for a single statement to span multiple lines:
不。
想知道其中的区别吗?从 JLS §14.5:块和语句:
No.
Want to know the difference? Start with the JLS §14.5: Blocks and Statements:
根据 Java 语法:
基于此您可以很容易地看到一条语句可以跨越多行,但单行也可以承载多个语句。另请注意,声明是一个非常广泛的术语。
According to Java grammar:
Based on this you can easily see that one statement can span multiple lines but also single line can host multiple statements. Also note that statement is a very broad term.
这行包含两个语句:
所以,一行不一定是一个语句......
This line includes two statements:
So, a line is not necessarily a statement...
仅通过惯例,并为了可读性。在 Java 中,语句以分号结束,或者在块的情况下,以一对大括号 ( { } ) 结束。
Only by common practice, and for readability. In Java statements are terminated with semicolons, or in the case of blocks, by pairs of curlybraces ( { } ).