Comments in Java

One of the questions I ran into when preparing for the Java Certification exam dealt with how Java handles nested comments. Since this isn’t something I usually do in my code I wasn’t sure how it worked. It turns out the results were different than I expected.

First lets look at the double slash comments. Since they mark everything to the end of a line as a comment there isn’t really any way to nest them. You can do something like this:

System.out.println("hi"); // Comments // More comments

But the “More comments” is never evaluated because the compiler simply skips the rest of the line. However with /* */ comments can be nested. First lets look at what happens when you nest // comments inside /* */ comments.


1 /* //important comment */
2 class Comment {
3   public static void main(String args[]){
4     System.out.println("test");
5   }
6 }

So what happens in this code? If the compiler sees the // and skips the rest of the line then the rest of the code will be marked as a comment because the closing */ will be skipped. This will produce a compile time error. So lets try compiling the code and see what happens.


mark-LT-G4:~/tmpjava marks$ javac Comment.java
mark-LT-G4:~/tmpjava marks$ java Comment
test
mark-LT-G4:~/tmpjava marks$

Ok it compiles and runs correctly. This means that the compiler must keep track of the fact that it is inside of a /* */ type comment and still looks for a */ even inside the // comment. But that brings up another question. What happens with code like this:

1 /* //comment */ class Comment {
2   public static void main(String args[]){
3     System.out.println("test");
4   }
5 }

If the compiler sees the // it should ignore the rest of the line. This should cause the compiler to fail because we’ll miss the class declaration. Here is what happens when we compile:

mark-LT-G4:~/tmpjava marks$ javac Comment.java
mark-LT-G4:~/tmpjava marks$ java Comment
test

Since it compiles we can see that the //comment is ignored when it is inside /* */ comment.

Now what happens if we nest /* */ comments? Take a look at the following code:

1 /* 
2   /*comment*/
3 */
4  class Comment {
5   public static void main(String args[]){
6     System.out.println("test");
7   }
8 }

The syntax high-lighting gives you a clue to what will happen, but lets look at the possibilities. First Java may keep track of nested comments and make sure that you balance /* /* with */ */. Second Java may have some type of “comments switch” that gets turned on when it sees a /* and ignores everything until it sees a */ which turns it back off. This would be similiar to what we experienced in the first example. If we try to compile we get:

mark-LT-G4:~/tmpjava marks$ javac Comment.java
Comment.java:3: 'class' or 'interface' expected
*/
^
1 error

The compiler is complaining about the second */. So it must ignore any subsequent /* it finds in the code once the “comments switch” is set to on.

Normally if you are writing code that requires this much knowledge of how comments work, you are probably doing something wrong. However, for passing the certification exam it can’t hurt to be prepared.

About 

Leave a Reply

Your email address will not be published. Required fields are marked *