Comments in Java

Java Comments are statements that are not executed by either Java compiler or Java interpreter. Java Comments are written anywhere in the Java file. Java Comments are used to block the Java code from execution without permanently deleting it.

Java Comments helps to write information, details and explanations within the Java file. Java Comments provides information about class, variables, methods, code logic details, etc.

These Java Comments are not included in the class file. Java comments are not available when a class file is de-compiled.



Types of Java Comments

Java supports three types of comments. Following are the three types.

  • Single Line Comments ( // … )
  • Multi Line Comments ( /* …..*/ )
  • Java Documentation Comments ( /** …..*/ )


Single Line Comments ( // … )

Java Single line comments are used to comment a single line. It begins with two forwarded slashes without any white space in between them. If the comments exceeds more than one line, each line should start with two forwarded slashes.

package com.yawintutor;

public class SingleLineComment {
	public static void main(String[] args) {
		
		// Welcome message is added in string
		String message ="Welcome to Yawin Tutor";
		System.out.println(message);
	}
}

Output

Welcome to Yawin Tutor

Example 2

package com.yawintutor;

public class SingleLineComment2 {
	public static void main(String[] args) {
		
		int price = 125; // price of my book
		System.out.println("Purchased Price of the Book after discount is Rs."+price);
	}
}

Output

Purchased Price of the Book after discount is Rs.125


Multi Line Comments ( /* …..*/ )

Java Multi-line comments are used to comment more than one line. It begins with one forwarded slash followed by a star (*). The commented statements are finished with a star ( *) followed by a forwarded slash. There may be various lines in the commented text.

package com.yawintutor;

public class MultiLineComment {
	public static void main(String[] args) {

		/*
		 * This is a multiline comment example.
		 * Below example shows Welcome message is
		 * added in string
		 */
		String message = "Welcome to Yawin Tutor";
		System.out.println(message);
	}
}

Output

Welcome to Yawin Tutor

Example 2

package com.yawintutor;

public class MultiLineComment2 {
	public static void main(String[] args) {

		/*
		  This is a multiline comment example.
		  Below example shows Welcome message is
		  added in string
		*/
		String message = "Welcome to Yawin Tutor";
		System.out.println(message);
	}
}

Output

Welcome to Yawin Tutor


Unicode characters in Java comments

Java compiler process the Unicode characters before processing java comments.

For example

\u002f\u002a Welcome to Yawin Tutor \u002a\u002f

is translated as

/* Welcome to Yawin Tutor */

On another example

String name = "Rama"; // name is added \u000a name = "Sita"; // now name is changed
System.out.println(name);

is translated as

String name = "Rama"; // name is added (new line added here)
name = "Sita"; // now name is changed
System.out.println(name);


Java Documentation Comments

Java Documentation Comments are used to generate Java API Documents. Java provides a mechanism for creating documentation about class, methods, variables, hierarchy of classes and class interdependence. Java Documentation Comments are used to provide information about classes, methods and variables in this API document.

package com.yawintutor;
/**
 * This class explains about how to use Documentation Comment
 * 
 * This class contains documentation comment in 
 * 	class level
 *  class properties level and
 *  class methods level
 */
public class DocumentationComment {
	/**
	 * This is a class property level documentation comment
	 */
	static String message ="Welcome to Yawin Tutor";

	/**
	 * This is a class method level documentation comment
	 * This method is main method of the java program
	 * 
	 */
	public static void main(String[] args) {
		System.out.println(message);
	}
}

Output

Welcome to Yawin Tutor


JavaDoc

Use javadoc command to generate java documentation

project/src>javadoc com/yawintutor/DocumentationComment.java

The above command create java document in the src folder. click on index.html file to view the java documentation.



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *