How to use comments and read errors

Write comments

Use // for one line and /* ... */ for a block. Comments should explain intent rather than repeat the code.

// Convert minutes to seconds.
int seconds = minutes * 60;

/* This example uses
   a block comment. */

Write documentation comments

A /** ... */ comment documents a class or method and can be processed by the javadoc tool.

/** Returns the larger of two values. */
static int larger(int a, int b) {
    return Math.max(a, b);
}

Read compiler errors

Start with the first error. Note the filename, line number, and caret, then inspect that line and the line immediately before it.

System.out.println("Hello")
// error: ';' expected

Read stack traces

For runtime failures, read the exception type and message, then find the first stack-trace line that names your class and source line.

String text = null;
System.out.println(text.length());
// NullPointerException points to this line