问题
Reading What is the best comment I've encouter this comment, upvoted 201 times:
A long time ago, I accidentally fixed a segfault in Java3D by adding a comment. It was 100% reproducible; if I removed the comment, it crashed. As long as the comment was there, it worked fine. I assume it was some bizarre timing issue, but I never did figure out exactly what was happening.
Is this possible?
If he was fixing a segfault in Java3D, I'm guessing he was writing code in Java or C/C++. I thought that in any of these languages, comments are simply erased before the compilation...
回答1:
I thought that in any of these languages, comments are simply erased before the compilation...
It might seem so but, unfortunately, this harmless-looking Hello World
program wouldn't even compile.
Why? Because of a comment. The culprit is the path the source. Specifically, \users
. The backslash \
is followed by the letter u
, denotes the start of a Unicode escape. The trailing sers
makes it a ill-formed Unicode escape. (It would have been ok if \u
was followed by 4 hex digits -- yes, you need that even in a comment.)
As such, the code wouldn't even compile due to the comment.
public class HelloWorld {
/**
* Source: C:\users\devnull\HelloWorld.java
*/
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
回答2:
As you state, in Java comments are not included in the compiled bytecode. For eaxmple, take the following class:
public class test {
public void methodWithoutComment() {
System.out.println("This method does not have a comment");
}
public void methodWithComment() {
// Method with comment
System.out.println("This method has a comment");
}
}
If we introspect the byte code with javap
, using javap -c test
, we can see the bytecode for both methods are identical - with the exception of the string that is outputted to the console:
public class test {
public test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void methodWithoutComment();
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3 // String This method does not have a comment
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
public void methodWithComment();
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #5 // String This method has a comment
5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
回答3:
Here are two explanations for this.
One, the reporter was wrong. They were not making the edit they believed they were making or were compiling or executing incorrectly or the error was erratic and coincided with their edits by chance, et cetera.
Two, the edit removed what looked like a comment, but it actually interacted with other syntax. E.g., consider:
/* Start comment.
…
/* Not a new comment. */
… Some code here …
/* Sometimes a comment. */
When the “Not a new comment” line is present, “Some code here” is source code that is not part of a comment. When the “Not a new comment” line is not present, “Some code here” is part of a comment.
回答4:
Don't think so. In order to run Java program, it should be compiled into bytecode so comments will be erased during the compilation and can't impact execution of the program.
来源:https://stackoverflow.com/questions/20120298/can-comments-make-any-difference-during-the-run-time