Java Reference
In-Depth Information
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i;
*/
As you can see, we failed to comment out the original snippet. On the bright side, the resulting code
contains a syntax error, so the compiler will tell us that we have a problem.
You may occasionally see a section of code that is disabled with an
if
statement whose
boolean
expression is the constant
false
:
// Code commented out with an if statement - doesn't always work!
if (false) {
/* Add the numbers from 1 to n */
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i;
}
The language specification recommends this as a technique for conditional compilation [JLS
14.21], but it is not well suited to commenting out code. It can't be used unless the code to be
disabled is a sequence of valid statements.
The best way to comment out a section of code is to use a sequence of single-line comments.
Most IDEs automate this process:
// Code commented out with a sequence of single-line comments
//
/* Add the numbers from 1 to n */
//
int sum = 0;
//
for (int i = 1; i <= n; i++)
Search WWH ::
Custom Search