Careeroza
Single-line and Multi-line Comments
What are Comments?
Comments are notes for humans. The compiler ignores them. Use them to explain why code does something, or to temporarily disable code while testing.
Single-line Comments
Example
Start with //. Everything after it on that line is ignored.
public class Main {
public static void main(String[] args) {
// This prints a greeting
System.out.println("Hello"); // comment at the end of a line
// System.out.println("This line is disabled");
}
}Output
Hello
Multi-line Comments
Example
Start with /* and end with */. Useful for longer explanations.
public class Main {
public static void main(String[] args) {
/*
Calculate GST for a product.
GST rate used here is 18 percent.
*/
double price = 1000;
double gst = price * 0.18;
System.out.println("GST: " + gst);
}
}Output
GST: 180.0
Note: Multi-line comments cannot be nested: /* outer /* inner */ still here */ is a compile error because the first */ ends the comment.