Commenting in your Java code is important so that other developers can easily read and understand your code. Comment implementations are intended for commenting out code or commenting on how the code works in a specific implementation. Comments give extra information if the code is difficult for other developers to understand.
In this short tutorial, I will explain the 3 types of comments in Java. Writing application code in Java is easy, especially when some parts of the code are commented to provide more information for other developers; not just for others, but also for you as a developer. The information provided below is perfect for you.
How to write comments in Java
It is simple to write comments in Java, you only need to understand the different types of comments in Java before you begin commenting your Java code. These types of comments are simple to learn and understand. Learn more below.
Types of comments in Java
These are the 3 types of comments in Java; Single-line, Multi-line, and Documentation comments. These comments are important and easy to learn and remember, especially for you as a beginner.
Single-Line Comment
Single-line comments start with two forward slashes //
. Java will ignore any text after the two forward slashes. This means that Java will not execute the text after these forward slashes. The Single-Line comment is widely used and easy to implement. Simply type the two forward slashes.
Example
// This is a Single-Line comment System.out.println("KensoftPH.com");
Multi-Line Comment
Multi-Line Comment is another name for (Block Comments). Multi-Line comments are used to describe files or to provide more explanation to the code. Java will ignore any text between /*
and */
. This means that the text between /*
and */
will not be executed by Java.
Example
/* This is an example of Multi-Line Comments */ System.out.println("KensoftPH.com");
Documentation Comment
It is one the three types of comments in Java. Documentation comment is commonly used to describe Java Classes, interfaces, constructors, and fields. If you wonder how it looks like, Documentation comments looks like this: /**...*/
. Learn more about the Documentation comments in Java.
Example
/** * This is an example of Documentation comment */ public class DocComment { ...