Learn to make Block Comments in Java for Multiple lines

Alex Rivers

In the technical world, comments are used for someone to make them understand what is going on in the code. In the case of Java code, we use block comments. We use block comments to describe what is happening in our code without it affecting anything in it. These block comments do not get implemented and do not get printed in the output, meaning the compiler does not compile it, and when it comes across any such comment, the compiler ignores it. Thus, the comments stay in the code only.

Types of Comments in Java

Here are the types of block comments in Java, namely:

  • Single-line Comments – //
  • Multi-line Comments – /* */
  • Documentation Comments -/** ….*/
  • Block Comments – /**…*/
  • End of line Comments – //

1. Single-line Comments

These describe the functioning of a code in a single line. They are used mainly by beginners. So, to convey general information about how any methods and functions work, one adds comments to the code.

//Comments here - Write here the functionality

Example:

public class SingleLine  {
public static void main(String args[])  {
 //An example to a print number in Java
int i=10;
System.out.println(i);
        }
}

The output will be 10. So, 10 will be printed on the screen.

2. Multi-Line Comments

A multi-line comment starts with /* and ends with */. You can add as many lines of comments as you want inside these symbols; there is no limit.

/*
Your
multi-line
comment comes here
*/

Example:

public class MultiLine {
public static void main(String args[])  {
/* 
initialize i with 1 and 
print the number in the next line
*/
int i=1;
System.out.println(i);
         }
}

The output will be: 1

3. Documentation Comments

They start with /** and end with */. We use these and create documentation API with the help of the Javadoc tool.

/**
 Your
documentation code
comes here
*/

Example:

public class SingleLine  {
public static void main(String args[])  {
 /**An example to 
a print number
 in Java
*/
int i=10;
System.out.println(i);
        }
}

The output will be 10.

4. Block Comments

They are similar to multi-line comments, and they also begin with /** and end with */. Adding Java block comments into your code is considered a good practice, and it clarifies what is going on inside the code.

They help in describing any Java document. The functionalities of the file codes can be added to them, making things manageable and easy to comprehend. When you open a file, you must see the block comment to understand what is going on in the code.

/*
*Block comment
* comes here
*/

Example

/*
*Name of the File: BlockCommentProgram.java
*Written by: Alex Rivers
*Description: This code contains a number that will be printed in the output. I have *given the number ''11' to be printed.
*/
public class BlockComments{
      public static void main(String args[]){
         int i=11;
             System.out.println(i);
       }
}

The output will be: 11

End of Line Comments

These are comments that you add at the end of any line in your code. It is depicted with //. So, you add the information at the end of one or every line.

This is a line of code written  //Your end of line comment comes here. 

Example:

public class EndOfLine{
       public static void main(String args[]){
             int i=44;
       System.out.println(i); //This line shall print the number in the output.
       }
}

This will print the number: 44

Conclusion

To sum it up, comments have been of great help in describing how your code works. It can be a line of code or the entire code, and comments like block comments have made things simple for every coder out there.

Happy Coding!

Also read: What is multithreading in C#?

Leave a Comment