Comments (Lesson 3)

Previously, you installed NetBeans and wrote your first program. Did you notice these lines of code? 

These are called comments.

Why are comments used?
Comments are basically used to add description to your code_any kind of description. They are actually not executed. They do not have any effect on how the program works. They only appear in your code.

Types of comments:

1. Single line comment:
As the name suggests, this comment is limited to a single line. You can add them anywhere in the following format.

//Comment goes here

Single line comment is sometimes called a double slash comment.

2. Multi-Line comments:
Again as the name suggests, these comments consist of multiple lines. You can use them if you want to add a big comment, that consists of multiple lines or contain a lot of text maybe? You can use them in following format anywhere in your comments.

/* A multi-line comment
   A big one
  It can go up to many lines

A multi-line comment is sometimes called a slash-star comment.

3. Documentation comment:
This one is different from the above ones. You are not going to learn about them now. By the way you can read this if you want to learn about documentation comments.

The following code illustrates the use of comments.

Actual use of comments:
It’s a big secret that I am going to reveal here. Read on your own risk. Actually programmers usually do not use comments to add description to their codes. They are used as a tricky tool to disable some part of your code_might be for some testing purposes.
As comments do not have any effect on the execution of the program nor they are executed. So if you want to save some lines of your code from “execution”, you can turn them into a comment.

For example, if it’s a single line, add double slash before it and it won’t execute.


And if there are multiple lines, surround them by /*….*/

Hope you understood the concept of comments and their real use.

Practice corner:
Do whatever you want to do with the comments.

2 thoughts on “Comments (Lesson 3)”

    • You should not put a comment on every line. It is also not necessary to use it very often. You can use comments as per your ease. You can write a code without comments also. Indeed most of our code goes comment-less.


Leave a Comment