Comments

There really isn’t all that much to say about comments. So naturally I am devoting an entire page to the subject.

Comments are notes left in code that the compiler program ignores. They are there for human benefit. There are single line and multi-line comments.

Single line comments are made by placing “//” in front of a line of text. While multi-line comments start with an opening tag “/*“, text in between, and end with a closing tag “*/“.

Here is an example with our “HelloWorld” program:

public class HelloWorld {
  public static void main(String[] args) {

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

    /*This is an example
      of multi-line
      comments. */
  }
}

You could use a single line comment at the end of a line of code. This technique is called, “end-line commenting.” But a lot of these comments can make code look disorganized and sloppy… So of course I do it all the time.

If you place “//” if front of a line of code, then that line won’t get compiled. This is a debugging trick called, “commenting out”. It is useful to test if a line of code is causing trouble.

You can use the multi-line comment tags to “comment out” entire sections of code. Or multi-line comment tags can be used to leave only a single line of comment. Or even a small comment in the middle of a line of code; a trick called “mid-line commenting.”

Fun Practice:

Take your “HelloWorld” program and pollute it with comments. Use both single and multi-line types. Then comment out an important piece of code and watch your program fail. It will be fun!

Next Topic: Integers, Floats, and Doubles

Comments

Popular Posts