Comments
- 100% developed as of Dec 31, 2012 Statements
- 100% developed as of Mar 10, 2013 Conditional blocks
- 100% developed as of Mar 10, 2013 Loop blocks
- 100% developed as of May 24, 2013 Boolean expressions
- 100% developed as of Feb 16, 2010 Variables
- 100% developed as of Mar 10, 2013 Primitive Types
- 100% developed as of Mar 10, 2013 Arithmetic expressions
- 100% developed as of May 24, 2013 Literals
- 100% developed as of Mar 10, 2013 Methods
- 100% developed as of May 24, 2013 String
- 100% developed as of Mar 10, 2013 Objects
- 100% developed as of Jul 5, 2012 Packages
- 100% developed as of Mar 10, 2013 Arrays
- 75% developed as of Jan 11, 2013 Mathematical functions
- 75% developed as of Jan 11, 2013 Large numbers
- 75% developed as of Jan 11, 2013 Random numbers
- 100% developed as of Apr 8, 2013 Unicode
- 100% developed as of Apr 8, 2013 Comments
- 100% developed as of Sep 27, 2007 Keywords
- 100% developed as of Aug 6, 2013 Coding conventions
- 0% developed as of Mar 26, 2018 Lambda expressions
A comment allows to insert text that will not be compiled nor interpreted. It can appear anywhere in the source code where whitespaces are allowed.
It is useful for explaining what the source code does by:
- explaining the adopted technical choice: why this given algorithm and not another, why calling this given method...
- explaining what should be done in the next steps (the TODO list): improvement, issue to fix...
- giving the required explanation to understand the code and be able to update it yourself later or by other developers.
It can also be used to make the compiler ignore a portion of code: temporary code for debugging, code under development...
Syntax
[edit | edit source ]The comments in Java use the same syntax as in C++.
An end-of-line comment starts with two slashes and ends with the end of the line. This syntax can be used on a single line too.
// A comment to give an example intn=10;// 10 articles
A comment on several lines is framed with '/' + '*' and '*' + '/'.
/* * This is a comment * on several lines. */ /* This also works; slash-star comments may be on a single line. */ /* Disable debugging code: int a = 10; while (a-- > 0) System.out.println("DEBUG: tab["+a+"]=" + tab[a]); */
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. Never nest a slash-star comment in another slash-star comment. If you accidentally nest such comments, you will probably get a syntax error from the compiler soon after the first star-slash sequence.
/* This comment appears to contain /* a nested comment. */ *Thecommentendsafterthefirststar-slashand *everythingafterthestar-slashsequenceisparsed *asnon-commentsource. */
If you need to have the sequence */ inside a comment you can use html numeric entities: */
.
Slash-star comments may also be placed between any Java tokens, though not recommended:
inti=/* maximum integer */Integer.MAX_VALUE;
However, comments are not parsed as comments when they occur in string literals.
Stringtext="/* This is not a comment. */";
It results in a 33 character string.
Question 3.26: Consider the following code:
int a = 0; // a = a + 1; a = a + 1; /* a = a + 1; */ a = a + 1; // /* a = a + 1; // */ a = a /*+ 1*/; a = a + 1; // a = a + 1; System.out.println("a=" + a);
What is printed in the standard output?
a=4
inta=0; // a = a + 1; a=a+1; /* a = a + 1; */ a=a+1; // /* a=a+1; // */ a=a/*+ 1*/; a=a+1;// a = a + 1; System.out.println("a="+a);
The highlighted lines are code lines but line 11 does nothing and only the first part of line 12 is code.
Comments and unicode
[edit | edit source ]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:
/*Thisisacomment.\u002a\u002f Stringstatement="This is not a comment.";
and is lexically equivalent to
/* This is a comment. */ Stringstatement="This is not a comment.";
(The '*'
character is Unicode 002A
and the '/'
character is Unicode 002F
.)
Similar caveats apply to newline characters in slash-slash comments.
For example:
That is because \u000a
is Unicode for a new line, making the compiler think that you have added a new line when you haven't.
Javadoc comments
[edit | edit source ]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. */