Comments and Slashes

Comments are very important in your programs. They are used to tell you what something does in English, and they also are used to disable parts of your program if you need to remove them temporarily. Here's how you use comments in Java:

public class CommentsAndSlashes
{
    public static void main( String[] args )
    {
        // A comment, this is so you can read your program later.
        // Anything after the // is ignored by Java.

        System.out.println( "I could have code like this." ); // and the comment after is ignored.

        // You can also use a comment to "disable" or comment out a piece of code:
        // System.out.println("This won't run.");

        System.out.println( "This will run." );
    }
}

From now on, I'm going to write code like this. It is important for you to understand that everything does not have to be literal. Your screen and program may visually look different, but what's important is the text you type into the file you're writing in your text editor. In fact, I could work with any text editor and the results would be the same.

What You Should See

I could have code like this.
This will run.

What You Should Do on Your Own

Assignments turned in without these things will not receive any points. For this exercise, try these things.

  1. Were you right about what the two slashes ('//') signify? Answer in a comment at the top of the file (above the 'public class' line).
  2. Add another comment at the very top of the file (above your answer to the previous question) with your name and today's date.



Copyright © 2010 Zed A. Shaw. Used by permission.

(The original Python version of this assignment is part of Zed Shaw's excellent Learn Python the Hard Way course and was translated to/reinterpreted for Java by Graham Mitchell.)