Java Comments Complete Tutorial

  • Post author:
  • Post category:Java

In this section we will learn what the Comments are in Java and how to use them.

What are comments in Java?

The source codes of programs get lengthy very quickly. Even a simple program can end up having over hundreds up to a thousand lines of code.

A lengthy source code can make it difficult for developers to read and make sense out of it. Even the developer that wrote the code, over time can forget what he or she wrote.

For this reason, we use comments in the source code to explain the purpose and its details. This will help everyone to understand how the program works and they can read and enhance the program easier in later time if needed.

How to comment in Java?

In Java programming we can put comments in places like:

  1. Above or below a method or class.
  2. Above or below a variable declaration, initialization and assignments.

Also we can put comments before or after a data-type name and a variable or method names as well as within its parentheses.

Note: compilers simply remove any comments they see in the program and compile the rest of the source code. This is because comments are for developers not compilers.

Let’s take a look at an example:

//The name of the class is Simple
public class Simple {
    //The class has only one method and that is 'main'
    public/*comments between access specifier and the static keyword*/ static void main(/* The method takes one argument and that is and array of strings*/String[] args){
        /*A variable of type String is created*/
        String/*comments between data-type and the variable*/ message = "Hello World";
        //The value of the variable 'message' is sent to the output stream.
        System.out.println(message);
        //End of the method
    }
    //End of the class
}

Output: Hello World

If you run the program above, the code will run without any problem even though we have mixed it with comments.

Types of Java Comments

There are three types of commenting in Java

1. Single Line Comment

2. Multi Line Comment

3. Documentation Comment

Single Line Comment in Java

This type of comment starts with two forward-slashes // and after that we can put comments.

Note: As the name suggests, this type of commenting is single line and if we need another line for comment, we need to start with two forward slashes // again.

Also, in single line comment, we can’t put any actual source code and expect to be compiled by the compiler because compilers simply ignore comments no-matter what in that comment is.

Example: creating single line comment in Java

class Main{
	public static void main(String[] args) {
		//This is a single line comment
		System.out.println("Hello World!");
	}
}

Multi Line Comment in Java

This type of commenting is not limited to just one line and we can put comments on multiple lines that follow each other.

Here’s the structure of a multiline comment:

/* body of the comment */

A multi-line comment starts with forward-slash followed by an asterisk followed by the actual comment, which can be in one or multiple lines, and then followed by another asterisk and ends with another forward-slash.

Remember: there’s no space between the forward-slash / and the asterisk * that comes before and after the comments.

Note: we can’t use comments between the names of Java keywords, variables and methods.

Example:

ret/*a comment between the 'return' keyword! */urn 0;

This is illegal and the compiler will return error because the `return` is a Java language keyword and we broke it by adding comments in between.

Example: creating multi line comment in Java

class Main{
	public static void main(String[] args) {
		/*
		This is a 
		Multiline 
		Comment
		*/
		System.out.println("Hello World!");
	}
}

Java Documentation Comment

This is covered in the Java Documentation Comment section.

Leave a Reply