Skip to main content

Comments


Introduction:
Welcome to a tutorial on the use of comments within Java.

What are comments?
Comments are just like the standard definition of comments anywhere in the world, they are used to explain something within the midst of the code. Comments are not read by Java and as such do not affect the program running.

When are comments used?
Comments are normally used to either make a note for the developer their self so they know where they need to go back to, or bookmark a specific location within a Java file. Comments are also very handy for large scripts, or for sending a block of sample code as you are able to annotate what each line does.

What is the format for a comment?
To submit a comment within Java, there are two ways to do this, the first one is a simple, one line comment which begins with two forward slashes ("//"). The second way allows multiple comment lines in a row which start with an asterix ("*") and is initialised by "/**" and terminated with "*/". Examples below.

Examples:
Here is an example of both types of comment...

  1. //System.out.println("1 This line is a comment and so is not ran by Java.");
  2. System.out.println("2 This line is not a comment and so is ran by Java.");
  3.  
  4. /**
  5.  * System.out.println("3 This line is a comment and so is not ran by Java.");
  6.  * System.out.println("3.5 This line is a comment and so is not ran by Java.");
  7.  */
  8. System.out.println("4 This line is not a comment and so is ran by Java.");

2 This line is not a comment and so is ran by Java.
4 This line is not a comment and so is ran by Java.

As you can see, the three lines in the comment types are not ran by Java.

Finished!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.