Java程序中的一行和语句一样吗?

发布于 2024-12-04 23:36:22 字数 55 浏览 2 评论 0原文

我是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情释 2024-12-11 23:36:22

不,我可以写:

int x = 1; int y = 2;

这是一行,两个陈述。

No. I can write:

int x = 1; int y = 2;

That's one line, and two statements.

独守阴晴ぅ圆缺 2024-12-11 23:36:22

不会。Java 编译器在编译程序时不会考虑行、间距或其他格式问题。它只是想看到每个语句末尾的;。此可以正常工作:

int i = 13; i += 23;

但是,执行此类操作可能(并且很可能会)导致源代码的可读性问题。因此,不建议这样做。

单个语句也可以跨越多行:

int i =
    13;

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:

int i = 13; i += 23;

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:

int i =
    13;
離殇 2024-12-11 23:36:22

在程序中,行和语句是一样的吗?

不。

想知道其中的区别吗?从 JLS §14.5:块和语句:

<前><代码>声明:
没有尾随子语句的语句
标签声明
IfThen语句
IfThenElse语句
While语句
For语句

没有尾随子语句的语句:
堵塞
空语句
表达式语句
断言语句
Switch语句
执行语句
中断语句
继续声明
退货声明
同步语句
抛出语句
Try语句

声明NoShortIf:
没有尾随子语句的语句
标签语句NoShortIf
IfThenElse语句NoShortIf
While语句NoShortIf
For语句NoShortIf

In a program, is a line the same thing as a statement?

No.

Want to know the difference? Start with the JLS §14.5: Blocks and Statements:

Statement:
        StatementWithoutTrailingSubstatement
        LabeledStatement
        IfThenStatement
        IfThenElseStatement
        WhileStatement
        ForStatement

StatementWithoutTrailingSubstatement:
        Block
        EmptyStatement
        ExpressionStatement
        AssertStatement
        SwitchStatement
        DoStatement
        BreakStatement
        ContinueStatement
        ReturnStatement
        SynchronizedStatement
        ThrowStatement
        TryStatement

StatementNoShortIf:
        StatementWithoutTrailingSubstatement
        LabeledStatementNoShortIf
        IfThenElseStatementNoShortIf
        WhileStatementNoShortIf
        ForStatementNoShortIf
恏ㄋ傷疤忘ㄋ疼 2024-12-11 23:36:22

根据 Java 语法

Statement:
    Block
    if ParExpression Statement [else Statement]
    for ( ForInitOpt   ;   [Expression]   ;   ForUpdateOpt ) Statement
    while ParExpression Statement
    do Statement while ParExpression   ; 
    try Block ( Catches | [Catches] finally Block )
    switch ParExpression { SwitchBlockStatementGroups }
    synchronized ParExpression Block
    return [Expression] ; 
    throw Expression   ; 
    break [Identifier]
    continue [Identifier]
    ; 
    ExpressionStatement
    Identifier   :   Statement

基于此您可以很容易地看到一条语句可以跨越多行,但单行也可以承载多个语句。另请注意,声明是一个非常广泛的术语。

According to Java grammar:

Statement:
    Block
    if ParExpression Statement [else Statement]
    for ( ForInitOpt   ;   [Expression]   ;   ForUpdateOpt ) Statement
    while ParExpression Statement
    do Statement while ParExpression   ; 
    try Block ( Catches | [Catches] finally Block )
    switch ParExpression { SwitchBlockStatementGroups }
    synchronized ParExpression Block
    return [Expression] ; 
    throw Expression   ; 
    break [Identifier]
    continue [Identifier]
    ; 
    ExpressionStatement
    Identifier   :   Statement

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.

扛刀软妹 2024-12-11 23:36:22

这行包含两个语句:

j = 5; j += 3;

所以,一行不一定是一个语句......

This line includes two statements:

j = 5; j += 3;

So, a line is not necessarily a statement...

落墨 2024-12-11 23:36:22

仅通过惯例,并为了可读性。在 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 ( { } ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文