JavaRush /Java Blog /Random EN /Comments in the Java language

Comments in the Java language

Published in the Random EN group
Comments in Java are, like in most other programming languages, characters that are ignored when a program is executed. Thus, you can add as many comments to the program as you need without fear of increasing its size. Comments are used to describe the subtleties of how a particular block of code, method, or class works. You can also use them if you want to leave messages for programmers who will work with this code in the future. Well, or for your own notes. Comments in Java - 1

Ways to highlight comments

The Java language has two ways to highlight comments in text.

Implementation comment (code comment)

  1. Commenting out a line

    This is the most commonly used type of comment. To write such a comment, you need to use two slashes //. In this case, the comment begins immediately after the characters //and continues to the end of the line.

    System.out.println("Hello, Java world!");
    // наш комментарий
  2. Commenting on a block of text

    If you need longer comments, you can start each line with characters. While it's more convenient to delimit comment blocks with /*and */.

    /*
     Пример простой программы на Java
    */
    
    public class SampleProgram
    {
     public static void main (String [] args)
     {
     System.out.println("Hello, Java world!");
     }
    }

    It should not be forgotten that comments separated by characters /*and */in the Java language cannot be nested. This means that a piece of code cannot be disabled by simply surrounding it with /*and */, because the code being disabled can itself contain the separators /*and */.

Documentation comment

To document methods, variables, or classes, a special way of highlighting text is used. This is done with /**and */. In this case, each line of commented text begins with *. If a method is documented, it is customary to describe its arguments and return value.
/**
* Метод возвращает максимальное meaning
* из трех переданных аргументов
* @param a - первый параметр
* @param b - второй параметр
* @param c - третий параметр
* @return - максимальный из параметров
*/
public int max(int a, int b, int c) {
   return Math.max(Math.max(a, b), c);
}
Comments in Java language - 2What else to read? For example, this material: Comments in Java: not everything is so simple . Or a lecture from the Java Syntax Pro quest about comments.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION