codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

5 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Comments
☕Java Programming

Java Comments

Updated 2026-05-12
8 min read

Java Comments

In programming, comments are essential tools that allow developers to explain their code. They help improve code readability, serve as a form of documentation, and can be used to temporarily disable parts of the code during debugging. In this tutorial, we'll explore different types of comments available in Java: single-line comments, multi-line comments, and Javadoc comments.

Introduction

Comments are non-executable lines that the Java compiler ignores. They play a crucial role in maintaining clean and understandable code, especially in collaborative projects where multiple developers work on the same codebase. Understanding how to use different types of comments effectively can significantly enhance your coding efficiency and the maintainability of your applications.

Single-Line Comments

A single-line comment is used to add brief notes or explanations right above a line of code. It starts with // and continues until the end of that line.

Example 1: Basic Single-Line Comment

SingleLineComment.java
1public class SingleLineComment {
2 public static void main(String[] args) {
3 // This is a single-line comment
4 System.out.println("Hello, World!"); // Print a message to the console
5 };
6}
Terminal
$ javac SingleLineComment.java
$ java SingleLineComment
Output
Hello, World!

In this example, the comment // This is a single-line comment explains what the code below it does. The second comment on the same line as the System.out.println statement provides additional context about its functionality.

Multi-Line Comments

A multi-line comment allows you to add comments that span multiple lines. It starts with /* and ends with */. This type of comment is useful for longer explanations or temporarily disabling blocks of code.

Example 2: Basic Multi-Line Comment

MultiLineComment.java
1public class MultiLineComment {
2 public static void main(String[] args) {
3 /*
4 * This is a multi-line comment.
5 * It can span multiple lines and is useful for detailed explanations.
6 */
7 System.out.println("Hello, World!");
8 }
9}
Terminal
$ javac MultiLineComment.java
$ java MultiLineComment
Output
Hello, World!

In this example, the multi-line comment provides a detailed explanation about what the code does. It's particularly useful when you need to explain complex logic or algorithms.

Javadoc Comments

Javadoc comments are a special type of multi-line comment used primarily for generating documentation. They start with /** and end with */. Javadoc comments can include tags like @param, @return, and @throws to describe method parameters, return values, and exceptions, respectively.

Example 3: Basic Javadoc Comment

JavadocComment.java
1/**
2* This class demonstrates the use of Javadoc comments.
3*/
4public class JavadocComment {
5 /**
6 * Prints a greeting message to the console.
7 *
8 * @param name The name of the person to greet.
9 */
10 public static void printGreeting(String name) {
11 System.out.println("Hello, " + name + "!");
12 }
13
14 public static void main(String[] args) {
15 // Call the method with a sample name
16 printGreeting("Alice");
17 }
18}
Terminal
$ javac JavadocComment.java
$ java JavadocComment
Output
Hello, Alice!

In this example, the Javadoc comment above the printGreeting method describes what the method does and explains its parameter. This information can be automatically extracted to generate comprehensive documentation for your code.

Practical Example

Let's create a simple Java program that demonstrates all three types of comments: single-line, multi-line, and Javadoc.

CommentsExample.java
1/**
2* This class demonstrates the use of different types of comments in Java.
3*/
4public class CommentsExample {
5 /**
6 * Calculates the sum of two integers.
7 *
8 * @param a The first integer.
9 * @param b The second integer.
10 * @return The sum of the two integers.
11 */
12 public static int add(int a, int b) {
13 // This is a single-line comment explaining the purpose of this line
14 return a + b; // Return the sum of a and b
15 }
16
17 /**
18 * Main method to execute the program.
19 *
20 * @param args Command-line arguments (not used in this example).
21 */
22 public static void main(String[] args) {
23 /*
24 * This multi-line comment explains that we are calling the add method
25 * with two integers and printing the result.
26 */
27 int result = add(5, 3);
28 System.out.println("The sum is: " + result);
29 }
30}
Terminal
$ javac CommentsExample.java
$ java CommentsExample
Output
The sum is: 8

In this practical example, we have used all three types of comments to explain different parts of the code. The Javadoc comment above the add method provides a detailed description of its functionality, while single-line and multi-line comments offer additional context within the method.

Summary

  • Single-Line Comments: Use // for brief notes or explanations on a single line.
  • Multi-Line Comments: Use /* ... */ for longer explanations or to disable blocks of code temporarily.
  • Javadoc Comments: Use /** ... */ for generating documentation. They include tags like @param, @return, and @throws.

What's Next?

Now that you understand how to use comments in Java, the next step is to learn about Java Variables. Variables are essential components of any programming language as they allow you to store and manipulate data. In the next tutorial, we'll explore different types of variables, their declarations, and usage in Java.

Stay tuned for more tutorials that will help you become proficient in Java programming!


PreviousJava OutputNext Java Variables

Recommended Gear

Java OutputJava Variables