Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Q.A Academy
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java For Automation

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java For Automation
  • What is Java Comments ?

What is Java Comments ?

What is Java Comments ?

In Java, comments are used to explain code and make it more readable. They are ignored by the Java compiler, meaning they do not affect the execution of the program. Java supports three types of comments:

  1. Single-Line Comments
  2. Multi-Line Comments
  3. Documentation Comments

1. Single-Line Comments #

Single-line comments are used for brief explanations or annotations within the code. They start with two forward slashes (//) and continue until the end of the line.

Example:

// This is a single-line comment
int x = 10; // This is also a single-line comment

2. Multi-Line Comments #

Multi-line comments are used for longer explanations or when commenting out blocks of code. They start with /* and end with */.

Example:

/* This is a multi-line comment
   It can span multiple lines */
int y = 20;

/* Multi-line comments can also be used
   to comment out blocks of code
int z = 30;
*/

3. Documentation Comments #

Documentation comments are special comments used to generate external documentation using the Javadoc tool. They start with /** and end with */. These comments can include HTML tags and special tags recognized by Javadoc, such as @param, @return, and @see.

Example:

/**
 * This is a documentation comment
 * It provides information about the class, method, or field
 * 
 * @param args the command line arguments
 */
public class MyClass {
    /**
     * This method performs addition
     * 
     * @param a the first number
     * @param b the second number
     * @return the sum of a and b
     */
    public int add(int a, int b) {
        return a + b;
    }
}

Summary #

  • Single-Line Comments (//): Used for short explanations or annotations.
  • Multi-Line Comments (/* … */): Used for longer explanations or to comment out blocks of code.
  • Documentation Comments (/** … */): Used for generating external documentation with Javadoc.

Why to use Comments?

Comments in code serve several important purposes:

1. Improving Code Readability #

Comments help make the code more understandable to other developers (or even the original developer, when revisiting the code after some time). They explain what the code does, why certain decisions were made, and how to use specific parts of the code.

Example:

// Calculate the area of a circle
double area = Math.PI * radius * radius;

2. Providing Documentation #

Comments, especially documentation comments, are used to generate official documentation for the code. This is particularly useful in large projects or libraries where end-users or other developers need to understand how to use the classes and methods.

Example:

/**
 * Calculates the area of a rectangle.
 *
 * @param length the length of the rectangle
 * @param width the width of the rectangle
 * @return the area of the rectangle
 */
public double calculateArea(double length, double width) {
    return length * width;
}

3. Explaining Complex Logic #

Sometimes, the logic in the code can be complex and not immediately clear. Comments can provide explanations or a step-by-step breakdown of what the code is doing.

Example:

// Using the Euclidean algorithm to find the greatest common divisor (GCD)
while (b != 0) {
    int temp = b;
    b = a % b;
    a = temp;
}

4. Marking To-Do Items #

Comments can be used to mark sections of the code that need further work, debugging, or review. This can be particularly helpful during development and maintenance.

Example:

// TODO: Handle edge cases for negative inputs
public int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    return n * factorial(n - 1);
}

5. Disabling Code #

Comments can temporarily disable parts of the code without deleting them. This is useful for debugging and testing different sections of the code.

Example:

// System.out.println("Debug: Value of x is " + x);

6. Providing Legal or License Information #

Comments can include legal disclaimers, license information, or author credits at the top of the file.

Example:

/*
 * This software is licensed under the MIT License.
 * See the LICENSE file for more details.
 */

Summary #

  • Improving Readability: Making the code easier to understand.
  • Providing Documentation: Generating documentation for users and developers.
  • Explaining Complex Logic: Clarifying intricate parts of the code.
  • Marking To-Do Items: Highlighting areas needing further work.
  • Disabling Code: Temporarily turning off parts of the code for testing.
  • Legal Information: Including licenses and credits.

By using comments effectively, developers can create code that is easier to maintain, understand, and collaborate on.

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
What is Java JDK, JRE and JVM ?What is Java Variable?
Table of Contents
  • 1. Single-Line Comments
  • 2. Multi-Line Comments
  • 3. Documentation Comments
  • Summary
  • 1. Improving Code Readability
  • 2. Providing Documentation
  • 3. Explaining Complex Logic
  • 4. Marking To-Do Items
  • 5. Disabling Code
  • 6. Providing Legal or License Information
  • Summary
© Copyright [2018-2025]. All Rights Reserved.