comments in java

Comments in Java

In the previous tutorial, you wrote your first Java program and got familiar with its basic structure. Now, let’s learn about comments in Java and how they help developers write clean and understandable code. 

What Are Comments in Java?

Comments in Java are non-executable statements written in the source code that explain, describe, or document the code.

  • Comments are ignored by the Java compiler.
  • They do not affect program execution.
  • They exist only to help developers understand the code better.
Comments in Java are just like notes in a textbook. Just like notes in a textbook help you understand a concept better, comments in Java help programmers understand code logic quickly. Comments play a very important role in writing clean, readable, and maintainable Java programs.
 
For example:
				
					class HelloWorld {
    public static void main(String[] args) {
        // This program prints a message
        System.out.println("Hello,World!");
    }
  }

				
			

Types of Comments in Java

Java supports three types of comments:

  • Single-line comments.
  • Multi-line comments.
  • Documentation (Javadoc) comments.

Let’s explore each one in detail.

1. Single-Line Comments (//)

Single-line comments in Java start and end on the same line. To write a single-line comment in Java, use the // symbol. Any text written after // is ignored by the compiler.

Example
				
					// This program prints a message
 System.out.println("Hello, World!");



				
			

You can also write a single-line comment after a statement:

				
					int userAge = 25; // storing user's age
				
			

Or you can also write a single-line comment to temporarily disable a line during debugging:

				
					//int userAge = 30;
 int userAge = 25;
				
			
When to Use Single-Line Comments
  • To explain a single line of code.
  • To add short notes.
  • To temporarily disable a line during debugging.

2. Multi-Line Comments (/* */)

Multi-line comments in Java are used when we want to write comments across multiple lines. To write a multi-line comment, we use the /* … */ symbols.

Example
				
					 /*
 This program calculates the sum
 of two numbers entered by the user
 */

 int sum = a + b;

				
			
When to Use Multi-Line Comments
  • To explain complex logic.
  • To write detailed explanations.
  • To comment out multiple lines of code at once.

3. Documentation Comments (Javadoc) (/** */)

Documentation comments are a special type of multi-line comments used to generate documentation automatically.

They start with /** and end with */.

Example
				
					/**
 * This class performs basic arithmetic operations
 * @author Harsh
 * @version 1.0
 */
 public class Calculator {
 }

				
			
Common Javadoc Tags
  • @param: describes a method parameter
  • @return: describes return value
  • @author: author of the code
  • @version: version information
When to Use Documentation Comments
  • To Document Class or Methods.
Method Example
				
					/**
    * Adds two integers
    * @param a first number
    * @param b second number
    * @return sum of a and b
    */
   public int add(int a, int b) {
       return a + b;
   }

				
			

How Java Compiler Handles Comments

  • The Java compiler completely ignores comments.
  • Comments are not included in bytecode.
  • Comments do not affect performance.
  • They are removed during compilation.
This means comments are only for humans, not for the machine.
 

Best Practices for Writing Comments in Java

Follow these best practices to write effective comments:

1. Write meaningful comments: Explain why something is done, not what is obvious.

				
					int x = 10; // assigning 10 to x (This is not necessary)
				
			

2. Keep comments updated: Outdated comments can confuse developers.

3. Use comments to explain complex logic: Especially helpful in algorithms and business logic.

				
					 /* This class handles complex order pricing logic
 * used in an e-commerce application.
 * Business Goal:
 * Apply 10% discount if order value is above ₹2,000
 * Maximum discount allowed is ₹300
 */


 public class DiscountCalculator {


    public static double calculateDiscount(double orderAmount) {


        // Default discount is zero
        double discount = 0;


        // Apply discount only if order crosses ₹2000
        if (orderAmount > 2000) {


           // Calculate 10% discount
           discount = orderAmount * 0.10;


           // Business rule: discount should not exceed ₹300
           if (discount > 300) {
               discount = 300;
           }
       }


       // Return final discount amount
       return discount;
   }
}

				
			

4. Use proper formatting: Well-formatted comments improve readability.

				
					/*
 This program calculates the sum
 of two numbers entered by the user
 */

 int a = 10;
 int b = 20;
 
 int sum = a + b;

				
			

Common Mistakes While Using Comments

Avoid these common errors:

  • Over-commenting simple code.
  • Commenting on bad code instead of fixing it.
  • Writing misleading or outdated comments.
  • Using comments instead of meaningful variable names.

Good Comments vs Bad Comments

Poor Commenting:

				
					// this method does something
  public void process() {
    // code here
 }

				
			

Good Commenting:

				
					// Calculates total price after applying discount
 public double calculateFinalPrice(double price, double discount) {
    return price - discount;
 }

				
			

 You should avoid comments when:

  • Code is self-explanatory.
  • Proper naming makes comments unnecessary.
  • Comments repeat what the code already says.

Comments in Java Interview Questions

Here are some common interview questions:

  • Are comments executed in Java?
  • What are the types of comments in Java?
  • What is Javadoc?
  • Can comments affect performance?
  • Can we write comments inside methods?

Summary

Let’s quickly recap:

  • Comments are ignored by the Java compiler.
  • Java supports three types of comments:
    • Single-line.
    • Multi-line.
    • Documentation (Javadoc). 
  • Comments improve readability and maintainability.
  • Use comments wisely and avoid over-commenting.

What’s Next?

Now that you understand Comments in Java, the next step is to learn how Java stores data.In the next tutorial, we’ll learn about variables in Java and how to use them in programs.

Java Tutorials

Stay Connected

Follow on Instagram for Java tips, coding reels, and much more. Subscribe to VairagiCodes YouTube Channel for learning the art of programming.

If you liked this article, then share it with your friends.

Related Tutorials

introduction to java programming
Introduction to Java Programming
  • June 27, 2025
  • Com 0

Introduction to Programming Programming is a process to communicate with electronic devices like computers, phones, etc. As a beginner, you…

Getting started with Java
Getting Started with Java
  • July 1, 2025
  • Com 0

Getting Started with Java Programming Java is one of the most popular and powerful programming languages. Java is a high-level,…

Hello World in Java Write Your First Program
Hello World in Java: Write Your First Java Program
  • January 17, 2026
  • Com 0

Hello World in Java In the previous tutorial, you learned how to install Java on your computer. Now, let’s write your…

Comments in java
Comments in Java : A Complete Beginner’s Guide
  • February 4, 2026
  • Com 0

Comments in Java are just like notes in a textbook. Just like notes in a textbook help you understand a...

Variables in Java Featured Image
Variables in Java: How To Use Variables
  • February 10, 2026
  • Com 0

Variable is like a container that stores data. At any point during a program, you can open these containers(variables in...

Data Types in Java
Data Types in Java – Explained with Real-Life Examples & Simple Analogies
  • February 22, 2026
  • Com 0

When you start learning Java, one of the most important concepts you encounter is data types in Java. Many beginners...

Get Your Free Java
Data Structures Cheat Sheet! 🎁

Download the ultimate cheat sheet covering the Top 10 Data Structures every Java developer must know — with real-life examples! Just enter your email and get the free pdf.

We respect your privacy. No spam. Read our privacy policy for more information.