When you create new projects, you should see that there is quite a lot of text that is greyed-out and contains slashes and asterisks. These are called comments and, when you run your program, the compiler will ignore them. Comments are usually nonexecutable statements which are meant for the programmers own convenience.
A comment is usually an explanation regarding the statement. Programmers use comments to allow readers of their code to understand better the code and how it works. Comments consist of English phrases that describe the purpose of the program, the use of identifiers, and the purpose of each program step. It also makes it easier to see where an error might show up.
Comments in Java are specified in one of three ways. The first two formats are borrowed directly from C++ and C. Comments in Java, as in C++ and C, do not end up in the executable bytecode. For this reason, comments can be used as often as deemed necessary, without the worry of inflated code.
There are three types of comments in java. They are:
Single line (or end-of line) comment
Single line comment allows only one line. It starts with a double slash symbol (//) and terminates at the end of the current line. The compiler ignores everything from // to the end of the line. For example:
// Calculate sum of two numbers
Multi line comment
If the comment exceeds more than one line, multiline comment is used. Java programmer can use C/C++ comment style that begins with delimiter /* and ends with */. All the text written between the delimiter is ignored by the compiler. This style of comments can be used on part of a line, a whole line or more commonly to define multi-line comment. For example.
sum of two numbers */
Documentation comment is used to generate document automatically. This comment style is new in Java. Such comments begin with delimiter /** and end with */ . The compiler also ignores this type of comments just like it ignores comments that use /* and */.The main purpose of this type of comment is to automatically generate program documentation. The javadoc tool reads these comments and uses them to prepare your program's documentation in HTML format. For example. /**The text enclosed here will be part of program documentation */.
A quick note about comments – keep them as short as possible and make sure they are clear on what is happening in the code; long, rambling comments that are full of unnecessary words are meaningless and make your code look even messier.