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.