- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Comments
Java comments are non-executable statements used for documentation and explanation purposes. While the Java compiler completely ignores these lines, they are arguably one of the most important parts of your codebase. They serve as "notes to self" and a roadmap for other developers, helping anyone reading the code understand the logic, intent, and complexity behind the instructions.
Single-Line Comments:
- Single-line comments start with //.
- They extend from the // marker to the end of the line and are ignored by the compiler.
- These are perfect for short descriptions or for "commenting out" a single line of code during debugging to see how the program behaves without it.
Multi-Line Comments:
- Multi-line comments are enclosed between /* and */.
- They can span multiple lines and are ignored by the compiler.
- These are frequently used for detailed explanations of complex algorithms or for adding copyright headers at the top of a source file.
*/. This can cause large chunks of your actual code to be treated as a comment, leading to confusing compiler errors.
Javadoc Comments:
- Javadoc comments are used to generate professional HTML documentation for Java code using the JDK's
javadoctool. - They start with /** and end with */.
- Javadoc comments can include special tags like @param (to describe method inputs), @return (to describe what a method sends back), and @throws to provide additional information about potential errors.
Example
In a real-world scenario, you might use these comments to explain a business rule or a specific calculation within a class:
/**
* Represents a simple greeting utility.
* This class demonstrates the three types of Java comments.
*
* @author rJTutorial Team
* @version 1.0
*/
public class HelloWorld {
/**
* The main entry point of the application.
* @param args Command-line arguments passed to the program.
*/
public static void main(String[] args) {
// Define the standard greeting message
String message = "Hello, World!";
/*
The following line prints the message to the console.
We use System.out.println for standard output.
*/
System.out.println(message); // This is an inline comment explaining this specific action
}
}
Summary
Java supports single-line comments (//), multi-line comments (/* */), and Javadoc comments (/** */). Comments are essential for improving code readability, providing explanations, and generating documentation for Java code. By mastering the art of commenting, you ensure that your code remains maintainable and accessible to your teammates—and to yourself when you revisit the project months later.