Unit 1: Comments

Table of Contents

  1. What are comments?
  2. Writing Comments

What are comments?

Comments are essentially notes that a programmer leaves in their source code. Comments are an important part of any good program because it makes the program more readable.

Comments are completely ignored by the computer when your program runs, so you can write whatever you want in them. Obviously, don’t use comments to write anything. Your comments should do things like describe what your program does. Some coders also use comments to write pseudocode, which is a technique coders use to plan out their programs.

When learning, you should make it a habit to add comments to all of your programs. It’s good programming style!

Writing Comments

There are 3 types of comments in Java: Single line comments, multi-line comments, and JavaDoc comments. We’ll get to JavaDocs later in Unit 4.

// This is a single line comment

/*
This is
a multi-line
comment.
*/

/**
 * This is a JavaDoc comment.
 * It can be multiple lines and is used
 * to describe structures in Java like
 * classes or methods, which we'll learn later.
 */

Notice that a single line comment starts with // and can only be on one line.

Also note that a multi-line comment begins with /* and ends with */, and it can span multiple lines.

You should write comments above code that the comment refers to, or on the side. For example:

// Prints Hello world! to the console
System.out.println("Hello world!");

or

System.out.println("Hello world!"); // Prints Hello world!