Comments in Java

Comments in Java are non-executable lines of code that are used to describe what the code is doing. They are ignored by the Java compiler and are not included in the resulting executable file.

Java supports two types of comments: single-line comments and multi-line comments.

Single-line comments start with two forward slashes (//) and continue to the end of the line. They are used to provide brief explanations for a single line of code.

Multi-line comments, on the other hand, are enclosed in /* and */ and can span multiple lines. They are typically used for longer explanations or to temporarily disable a block of code.

It is important to use comments in Java to improve the readability and maintainability of your code. Comments can help others understand what your code is doing and can also remind you of your thought process when you come back to the code later. However, it is important not to overuse comments and to keep them up to date as the code changes.

In addition to these standard comments, Java also supports documentation comments, which are used to generate documentation for Java programs. These comments start with /** and end with */ and can include tags such as @param and @return to document the purpose and usage of methods and classes.

By using comments effectively in Java, you can improve the quality and understandability of your code, making it easier to work with and maintain over time.

Syntax

// single-line comment

/*
 mulit-line 
 comment
 */

/**
 * documentation comments (Javadoc)
 * Javadoc is a documentation generator
 * some common elements are included, like:
 * 
 * @author (Author name)
 * @version (Version number)
 * @param (Parameters to add)
 * @return (return type/value)
 * @exception (or @throws)
 * @see
 * @since
 * @serial (or @serialField or @serialData)
 * @deprecated 
 */

Code Example

  1. The code defines a class called Comments using the class keyword.
  2. Within the class, the add method is declared using the private static int keywords. The method takes two integer arguments, x and y, and returns their sum using the return statement. The method is also documented using a JavaDoc comment, which provides information about the method’s purpose, arguments, and return value.
  3. The main method is declared as public static void, which means it’s a public method that doesn’t return a value and can be invoked without creating an instance of the Comments class. The method takes an array of strings, args, as its argument, which contains the command-line arguments passed to the Java program when it’s run.
  4. Within the main method, two variables x and y are declared and initialized to 2 and 5, respectively.
  5. The System.out.print method is called with the result of add(x, y) as its argument, which will print the result of adding x and y to the console.
public class Comments {
	/**
	 * add function
	 * @param x integer value to add
	 * @param y integer value to add
	 * @return addition of this two parameters
	 */
	private static int add(int x, int y) {
		return x + y;
	}

	/* args contains the command-line arguments
	passed to the Java program upon invocation.*/
	public static void main(String[] args) {
		// Variable declaration and initialization
		int x = 2, y = 5;

		// Output
		System.out.print(add(x, y));
	}
}
Output
7