• TestingDocs
TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Types of Java Comments

Introduction

Java comments are useful explanations to make the program understandable to others. Comments are ignored by the compiler and interpreter but are useful to programmers and other humans. Comments are not executed. The Java programming language supports three kinds of comments:

  • Single Line comment   
  • Multi-line comment
  • Documentation Comment

Single Line comment

Single line comment as the name indicates can only have a single line. Syntax for a single line comment:

Syntax

// comment text short and crispy

 

Single Line Comment Java

 

A single line comment starts with //

Make sure the comment is short and crispy to convey the message. For example,

//declare variables

We use single line comment to increase the readability of the Java program. For example, when the program has multiple nested blocks, we use the comments to denote where the code block ends.

 

package com.testingdocs.demo;

public class SampleProgram {
 //Program Entry point
 public static void main(String args[]) {
 String output="Learn Java - www.TestingDocs.com";
 System.out.println(output);
 } // end main
} // end class

Notice how we have used comments to denote the end of code blocks to avoid confusion. This would be extremely useful when we use nested control, loop structures.

Multi-line comment

A multi-line comment can span across multiple lines. We can use multi-line comments for lengthy comments like program headers, license terms, code explanations etc.

Syntax

/* multi-line comment text */

Example

/*
This is a multi-line comment. This comment
can have multiple lines.

To increase readability, * is added for all the lines
*/

Java Comments

 

IDE Code editors generally change the color of the comments to increase readability of the Java program.

Documentation comment

Java documentation comments add to the HTML documentation created by the javadoc tool.

Syntax

/** java documentation comment */

Example

/**
This indicates a documentation comment. The compiler ignores this kind of comment too. The javadoc tool uses documentation comments when preparing automatically generated documentation.
*/

 

Notice that two ** in the first line. More information on documentation comment:

https://www.testingdocs.com/java-documentation-comments/

 

That’s it. We have successfully gone through different types of Java comments.

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Java Performance

Java Tutorials /

Improving Java Performance with Multithreading

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

‹ Get Substring of Given String in Java› Java Inheritance

Recent Posts

  • How to secure your SQL Database: Tips and Tricks
  • Shaping the Future of Development: Exploring Key Trends in Software Engineering
  • Improving Java Performance with Multithreading
  • Difference between PHP and JavaScript?
  • Bing Conversation Styles
  • ChatGPT Introduction
  • Open Source AI Frameworks
  • Artificial Intelligence Tools
  • Top AI Music Applications
  • Top AI Website Design Tools

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com