Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java include comments on the compiled code?

Well, the title is self-explanatory. I wondered this while compiling a project which has a lot of lines commented. It's a bit silly because the jar file wouldn't increase much (some bytes) but I'm curious if this could affect a program with a lot of code and comments.

Thanks

like image 979
oli206 Avatar asked Oct 27 '10 10:10

oli206


People also ask

Are comments in compiled code?

A token can be an identifier, a literal value, a reserved word, or an operator. Comments and whitespace are mostly ignored during this phase. They are only used to separate different tokens. In the next steps, there is no concept for a comment or a whitespace, so yes, they are removed while compiling.

Does Java compiler remove comments?

Class files don't have comments, they are removed by the compiler.

What happens when Java code is compiled?

Java source code is compiled into bytecode when we use the javac compiler. The bytecode gets saved on the disk with the file extension . class . When the program is to be run, the bytecode is converted, using the just-in-time (JIT) compiler.

How are comments in a Java program treated by the compiler?

Do They Affect How the Program Runs? Implementation comments in Java code are only there for humans to read. Java compilers don't care about them and when compiling the program, they just skip over them. The size and efficiency of your compiled program will not be affected by the number of comments in your source code.


4 Answers

No, comments normally are stripped out in any language (not just in Java). They have no representative in byte code. What stays in there, is annotated stuff and such

like image 150
Scoregraphic Avatar answered Oct 05 '22 14:10

Scoregraphic


It would not affect the execution or performance (unless you have commented out the wrong statements :-)

But it definitely would affect readability. There is no reason to have large blocks of commented out code (in production), version control is the way to go

See this question also.Question closed.

like image 30
Nivas Avatar answered Oct 05 '22 14:10

Nivas


No, documentation isn't included in the compiled class file.

The javadocs can be generated with the javadoc program. All javadoc will be converterd to HTML.

like image 20
Salandur Avatar answered Oct 05 '22 16:10

Salandur


No, this is not added.

However, you should consider to kill dead code (i.e. commented code, but also unused code). If you think that "maybe I will need that method again", simply delete the method, and if you really need this method some day, use you SCM tool (Subversion, Git, CVS or whatever) to retrieve this old code...

like image 20
Romain Linsolas Avatar answered Oct 05 '22 14:10

Romain Linsolas