Lesson 5 +10 XP

Java Comments

Java Comments

Comments are lines of text in your code that are ignored by the compiler. They help you and others understand what the code does.

Single-line comments

Use two slashes // to comment out a single line:

// This is a comment
System.out.println("Hello World");

You can also put a comment at the end of a line:

System.out.println("Hello World"); // this is a comment

Multi-line comments

Use / ... / to comment out several lines:

/*
  The code below prints a greeting.
  It is the first line shown to the user.
*/
System.out.println("Hello World");

Why use comments

  • Explain tricky parts of the code.
  • Remind yourself what a section does.
  • Help other developers read your code.
  • Temporarily disable a line while testing.

Comments are not executed

The compiler skips everything inside a comment, so comments never change how the program runs.

TL;DR

  • // makes a single-line comment.
  • / ... / makes a multi-line comment.
  • Comments are ignored by the compiler.