Java Comments


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


Single-line Comments (//)

  • Use for brief, single-line explanations.
  • Starts with //. Anything after // on the same line is ignored by the compiler.
Example
// This is a single-line comment
System.out.println("Hello, World!"); // Print a message to the console

Multi-line Comments (/* ... */)

  • Use for longer explanations or to temporarily block out multiple lines of code.
  • Starts with /* and ends with */. Everything between these markers is ignored by the compiler.
Example
/*
This is a multi-line comment.
It can span multiple lines.
*/
System.out.println("Multi-line comment example.");

Documentation Comments (/** ... */)

  • Used to generate API documentation with tools like Javadoc.
  • Starts with /** and ends with */. Contains special tags to describe classes, methods, and parameters.
Example
/**
* This class demonstrates Java comments.
* It contains a method to add two numbers.
*/
public class CommentsExample {

/**
* Adds two integers and returns the result.
*
* @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;
}

public static void main(String[] args) {
CommentsExample example = new CommentsExample();
System.out.println("Sum: " + example.add(5, 10)); // Prints the sum
}
}

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.