Website For Developers

Comments in Java



Single line comments in Java

Example of single line comments in Java

public class SingleLineComment {

   public static void main (String [] args) {
     // This is an example of single line comment

     System.out.println ( "Hello ");
     // System.out.println ( "Hello World" );
   }
}

Output

Hello

Note: Compiler ignored the second print statement because that line was commented.

Many or multi line comments in Java

Example of multi line comments in Java

public class MultiLineComment {

   public static void main (String [] args) {
     /*This is an example of
     multi lines comment*/

     /* System.out.println ( "Hello ");
     System.out.println ( "Hello World" ); */
   }
}

Output

Note: There is nothing to show as an Output Because Compiler ignored those lines as they are commented.
Tip: For best practice in programming, use comments frequently. But while commenting keep in mind that you are not teaching the line by line code to someone.