Java Comments

Java comments are used to ignore lines or blocks of code by the compiler means when the compiler compiles a Java program it ignores those codes that are commented.

Java Comments are used in different - different conditions, In this topic we will see when and how to use comments, so let’s first know why to use Java comments.

Java Comments

Why Java Comments

This is a very common thing to know why we use comments in Java programming. Basically, java comments are used as remarks in programming, suppose a group of people are developing software and each person is given some parts to code, and last all parts have to combine. There may be many methods and variables in each part, so developers put a comment about what that method is. Usually, developers create a comment and write a remark about the use of that method or variable that he has created so that other developers can easily understand it.

Type of Comments in Java

There are three types of comments in java which are listed below.

  1. Single-Line comment
  2. Multi-Line Comment
  3. Documentation Comment

Single-Line comment

As the name indicates, it is used to ignore a single-line code or to write a remark in a single line. It is denoted by // and after it type all your remarks. There is no limit to how many single-line comments can be used in a program. Let’s take a look with the help of an example.

For Example


In the above program all lines that are stat from // are single-line commented, the compiler ignores all these lines in the compilation.

Actually, it’s not about the line, it’s about the remaining code in a line after the // symbol.

If a line contains some code and then // and then some other code, in this case compiler only ignores the codes that are after the // symbol in that line.

Output

Multi-Line Comment in Java

Multi-line comments are also used to ignore or for remarks in a program, it can be used in multiple lines at once. It starts from /* and ends by */. All code and text between these are ignored by the compiler at the time of compilation. Let’s see it in action in the below example.

For Example

Output

Imp: Closing */ symbol is must otherwise it will comment all code after /* symbol.

Documentation Comment

Comment used to produce an HTML file that documents your program. The documentation comment begins with like this /** and ends with */:


Remarks

  • There is no limit to how many comments you are using in a program.
  • There is no limit to how many types are comments you are using in a program.
  • You can use a multi-line comment as a single-line comment, just put both opening /* and closing */ in the same line.
  • The use of comments depends on the developer for what reason he is using it.

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

© 2023 GDATAMART.COM (All Rights Reserved)