Tuesday, September 16, 2014

Java comments

Comments are notes written to a code for documentation purposes. Those text are not part of the Java program and does not affect the flow of the program but also important. Comments are useful so that you can document every part of your code especially when it is more than a hundred lines. Comments make your code readable and understandable.

Java supports three types of comments: C++-style single line comments, C-style multiline comments and special javadoc comments. C++ Style comments starts with //. All the text after // are treated as comments. For example,

// This is a C++ style or single line comments
C-style comments or also called multiline comments starts with a /* and ends with a */. All text in between the two delimeters are treated as comments. Unlike C++ style comments, it can span multiple lines. For example,

/* this is an exmaple of a 
    C style or multiline comments */
Special Javadoc comments are used for generating an HTML documentation for your Java programs. You can create javadoc comments by starting the line with /** and ending it with */. Like C-style comments, it can also span lines. It can also contain certain tags to add more information to your comments. For example,

 This is an example of special java doc comments used for \n
 generating an html documentation. It uses tags like: 
 @author Noel N. Mosura 
 @version 1.2

It is a good practice to get into the habit of putting comments into your source code to enhance its readability for yourself and also to other programmers who are members of the development team especially if you are working as a team. It is not always instantly clear what actions a piece of code is performing. A few explanatory lines can drastically reduce the amount of time it takes to understand the source code.

No comments:

Post a Comment