x

Java – Comments

Prev     Next

  • Comments are description given by the programmer within the program. So that if another programmer looks at the program,he can easily understand the program by reading the comments.
  • The characters or words or anything which are given in comments section won’t be considered by java compiler for compilation process and will be ignored by java compiler during compilation.

Example program for java comments:

Comments can be given in three ways inside a java program. They are,


  1. Single line comment
  2. Multi line comment
  3. Documentation comments

1. Single line comment in Java:

It starts with two forward slashes and continues to the end of the line.

Syntax:


 // This comment extends to the end of the line.
String welcome = “Hi”; //The text to print

2. Multi line comment in Java:

Multi line comment starts with forward slash followed by an asterisk, and ends with an asterisk followed by a forward slash.

Syntax:

/*  This is a
comment used for
multiple lines */
/* This comment can be used for single line also */

3. Documentation comments in Java:

  • Documentation comments starts with a forward slash followed by two asterisks, and ends with an asterisk followed by a forward slash. This comment contains descriptions about the code and is used to create API document.
  • This comment is usually added in the beginning of the java file explaining why this program is designed and how to use it.

Syntax:

/** This is a
documentation comment */

Prev     Next



Like it? Please Spread the word!