Java Programming/Syntax/Comments
Contents |
[edit] Comments
Comments can appear anywhere in the source code where whitespace is allowed. Java comments can be written in two ways: slash-slash (// ...
) comments and slash-star (/* ... */
) comments
[edit] Slash-slash comments
![]() |
// This comment extends to the end of the line.
// Two forward slashes (with no whitespace between them) begin such comments. // This type of comment is called a "slash-slash" comment |
[edit] Slash-star comments
![]() |
/* This comment, a "slash-star" comment, spans multiple lines.
* It begins with the slash-star sequence (with no space between * the '/' and '*' characters) and extends to the star-slash sequence. * By convention, subsequent lines of slash-star comments * begin with a star aligned under the star in the open comment * sequence, but this is not required. */ |
![]() |
/* This also works; slash-star comments may be on a single line */
|
![]() |
/* So does this.
There are no stars on the subsequent lines */ |
Slash-star comments may also be placed between any Java tokens:
![]() |
int i = /* maximum integer */ Integer.MAX_VALUE;
|
[edit] Javadoc comments
Javadoc comments are a special case of slash-star comments.
![]() |
/**
* Comments which start with slash-star-star are JavaDoc comments. * These are used to extract documentation from the Java source. * More on javadoc will be covered later. */ |
[edit] Comments may not be nested
Java slash-star comments may not be nested.
![]() |
/* This comment appears to contain /* a nested comment. */
* The comment ends after the first star-slash and * everything after the star-slash sequence is parsed * as non-comment source. */ |
If you accidentally nest such comments, you will probably get a syntax error from the compiler soon after the first star-slash sequence. If you need to have the sequence */ inside a comment you can use html numeric entities: */.
[edit] Comments in String Literals
Comments are not parsed as comments when they occur in String literals.
![]() |
String text = "/* Ceci n’est pas un commentaire. */";
|
results in a 33 character string.
[edit] Unicode Characters in Comments
Be aware that Java still interprets Unicode sequences within comments. For example, the Unicode sequence \u002a\u002f
(whose codepoints correspond to */) is processed early in the Java compiler's lexical scanning of the source file, even before comments are processed, so this is a valid star-slash comment in Java:
![]() |
/* Ceci est un commentaire. \u002a\u002f
String statement = "Ceci n’est pas un commentaire."; |
and is lexically equivalent to
![]() |
/* Ceci est un commentaire. */
String statement = "/* Ceci n’est pas un commentaire. */"; |
(The '*'
character is Unicode 002A
and the '/' character is Unicode 002F).
Similar caveats apply to newline characters in slash-slash comments.
For example:
![]() |
//This is a single line comment \u000a This is code
|
That is because the \u000a is the Unicode for a new line, making the compiler think that you have added a new line when you haven't.
This may be useful when declaring more than one thing on a line and you still wish to use // type comments
![]() |
int x = 0; //X is the value of the carr \u000a int y=0; //Y is the interest
|
It is recommended that you use the /* and */ style comments instead or just separate them into two lines, it won't hurt and it will make your code more readable.