Wednesday 5 February 2014

Comments in java

Comments in java, makes the program more readable.

Suppose you written thousand lines of program, with out proper documentation, after some time, for the owner of the application also, it is very difficult to figure out what was written.

Comments solve this problem :
By using comments, you can document your code at the time of writing program. Comments won't affect your program execution. Comments simply document your code.

Java supports 3 types of comments

     Single line comment :
          Single line comments starts with //
          // It is a single line comment

     Multi Line comment
     Multi line comments are encoded in /* */
          /* I am a mulltiline comment */

     javadoc tool uses doc comments when preparing automatically generated documentation.

     /**
     * Java doc comments
     */

Example
/**
* Author : Krishna
* Date : 5th Feb 2014
*/
class CommentsEx{
 public static void main(String args[]){
  int marks; /* variable marks represents the marks
                  obtained by a student */
  marks = 70;
  System.out.println(marks); //printing to console
 }
}

Output
70

Some Notable points

1. Giving more comments also makes your program not readable, use whenever needed.

2. Use meaning full names to variables like noOfMonths. avgSalary etc., these reduce to comment them explicitly

3. Use meaning full names to methods like sortAscend, sortDescend, searchElement etc., these reduce to comment them explicitly


How to iterate over a two dimensional array in Java?                                                 Documentation Comments                                                 Home

No comments:

Post a Comment