Java Tutorial
Java Comments
Comments are for people. The compiler skips // lines and /* blocks */.
The compiler ignores comments
A comment is text you leave in the source so a human can follow the program. javac does not compile it. It does not change output. Use comments to record why a line exists, not to repeat what the code already shows.
Example
public class Main {
public static void main(String[] args) {
// This program greets the user once.
System.out.println("Hello");
}
}Line comments with //
Two slashes start a comment that runs to the end of that line. You can put // on its own line or after a statement.
Example
public class Main {
public static void main(String[] args) {
int n = 10; // starting inventory
System.out.println(n);
// System.out.println("skip this line while testing");
System.out.println("still running");
}
}Block comments with /* */
/* starts a comment that can span several lines. */ ends it. Blocks do not nest. Prefer // on each line when you disable a chunk of code.
Example
public class Main {
public static void main(String[] args) {
/*
Print a short header, then a number.
*/
System.out.println("total");
System.out.println(42);
}
}What to write
| Weak | Useful |
|---|---|
count = count + 1; // increment count | count = count + 1; // skip the header row |
price = price * 0.9; // multiply | price = price * 0.9; // 10 percent loyalty discount |
Javadoc: /** */
A comment that starts with /** just above a class or method is a Javadoc comment. Tools turn these into browsable documentation, and editors show them as pop-up help when you hover a method. You will see them everywhere in real Java.
Example
public class Main {
/** Returns the sum of a and b. */
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(2, 3));
}
}Key Takeaways
//comments to the end of the line;/* ... */spans lines.- The compiler ignores every comment — they are for humans.
- Explain why, not what the code obviously already says.
/** ... */Javadoc above a method becomes documentation and editor hover-help.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Annotate a reagent
A comment is not executed. It tells the next person which bottle and concentration the number came from.
In a lab notebook the same habit stops someone treating 0.100 as volume when it was molarity.
Example
public class Main {
public static void main(String[] args) {
// HCl stock: 0.100 mol/L
double conc = 0.100;
System.out.println(conc);
}
}