• Register
140 points
4

In this article, you will learn how to comment out a block of code which is explained step by step below with few examples. It is explained in eclipse or the general IDE.

What is a comment?

Comments are basically used to make users understand the code and it makes code debugging simpler. One can definitely use this comment,  to prevent a set of code from compilation and only allow a particular block of code to execute. 

Types of Comments

There are mainly two types of comments used in JAVA or any other programming language. They are mentioned below.

  1. Single -line Comments
  2. Multi-line Comments

A single-line comment is used for a single line code but not to a set of lines because it applies only to a particular line. Single-line comments use two forward slashes ( // )  at the start of the comment line and it does not need to be included at the end. As JAVA compiler on reading two consecutive forward slashes, it jumps to the next line directly.

While a multi-line comment is used to hide or prevent those lines of code from execution. More than one line is called a multi-line and hence this multi-line comments use a combination of ( /*) at the front of a line and  (*/) at the end of a set of lines. 

So any text between these (/*) and (*/) is termed as a comment and this is used for multi-line commenting.

Approach

  • If you want to comment out a single line of code then using two forward slashes (//) placed at the front of the line will suffice as it refers to single-line commenting.
  • If you want to comment on an entire block of code then the usage of (/*) & (*/) is applied which refers to multi-line commenting.
  • Refer to the below examples that will help you with commenting on a block of code.
  • In eclipse, select the required line to be commented out, shortcut  ctrl + /  on clicking will provide with single-line comment.
  • Select the set of lines to comment and press ctrl + shift + / to make a multi-line comment in eclispe. This is far simple when compared to normal IDE.

Program

public class MyClass 

{
public static void main(String[] args)

{

// This is a single-line comment

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

}

}
public class MyClass 

{
public static void main(String[] args)

{

/*This is a multi-line comment
   showing you a block of code 
   which has a set of lines */

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

}

}

Output

Hello World


 

 

140 points
4