URL在Java代码中编译时没有错误:为什么javac编译器会忽略它?

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

URL in Java code compiles without error: Why is the javac compiler ignoring it?

问题

我在测试一些代码时犯了一个错误,忘记删除只包含URL的一行。代码看起来像这样:

class Example {
    public static void main(String[] args) {
        https://example.com
        System.out.println("Example");
    }
}

我假设编译器会失败,因为main方法的第一行包含非Java代码。然而,代码编译并正常工作。

我以为//符号后的所有内容都是注释,并预期以下代码将无法编译通过:

https:
System.out.println("Example");

令我惊讶的是,它竟然可以编译通过。

然而,以下代码产生了编译错误,如预期的那样:

class Example {
    public static void main(String[] args) {
        https://example.com
    }
}

编译器错误信息如下:

/Example.java:4: error: illegal start of statement
    }
    ^
1 error

有人可以解释为什么以及在什么情况下javac编译器会忽略包含URL的行吗?我在线编译代码在https://www.jdoodle.com/上,也在本地手动编译,行为总是相同的。我使用了Java8和Java17。

我注意到这行被忽略是因为在IntelliJ中我可以在反编译的代码中检查到该行不存在。

英文:

I made a mistake while testing some code and forgot to delete a line that contained only a URL. The code looked something like this:

class Example {
    public static void main(String[] args) {
        https://example.com
        System.out.println("Example");
    }
}

I assumed that the compiler would fail because a non-Java code was present in the first line of the main method. However, the code compiled and worked fine.

I thought that everything after the // symbol was a comment and expected the following code to fail compilation:

https:
System.out.println("Example");

To my surprise, it compiled without error.

However, the following code produced a compile error, as expected:

class Example {
    public static void main(String[] args) {
        https://example.com
    }
}

Compiler error

/Example.java:4: error: illegal start of statement
    }
    ^
1 error

Can anyone explain why and under what circumstances the javac compiler ignores a line that contains a URL?

I compile the code online in https://www.jdoodle.com/ locally by hand and the behavior is the same always. I used Java8 and Java17.

I saw that the line is ignored because in intellij I could check in the decompiled code that the line is not there.

答案1

得分: 1

以下是已翻译的内容:

这实际上是有效的Java代码:

https://example.com
System.out.println("Example");

https: 是一个标签

// 是一行的注释

System.out.println("Example"); 是带有 https: 标签的语句

带标签的语句是Java中的一种不常见的构造,通常与带标签的break语句一起使用。以下是一个(略微)有用的示例:

out: while (true) {
int i = Random.nextInt(10);
while (true) {
int j = Random.nextInt(10);
if (i == j) break out;
}
}

break out; 中断了两个while循环。


你的第二个例子不是有效的Java,因为标签后面没有语句。

英文:

It is actually valid Java code:

    https://example.com
    System.out.println("Example");

The https: is a label

The // is a comment ... to the end of the line

The System.out.println("Example"); is the statement that is labelled with https:

Labelled statements are an obscure Java construct that is used in conjunction with the break to label statement. Here's a (marginally) useful example:

out: while (true) {
        int i = Random.nextInt(10);
        while (true) {
            int j = Random.nextInt(10);
            if (i == j) break out;
        }
     }

The break out; breaks the two while loops.


Your second example is not valid Java because there isn't a statement after the label.

huangapple
  • 本文由 发表于 2023年4月20日 00:02:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056617.html
匿名

发表评论

匿名网友

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

确定