Java comment dilemma

This was a question about my appointment:

Which of the following is an unacceptable way of commenting? What for?

  • /** a comment */
  • /* a comment */
  • //a comment
  • // comment to comment
  • / * comment comment * /

Honestly, they all look good. But I thought it could be / ** comment * / because it is not a multi-line example, but its purpose is documentation. What do you think? This is the only question that is hard for me. Any help would be appreciated! Thanks.

+4
source share
4 answers

In terms of grammar, none of the above ways of specifying comments is acceptable. However, to make it easier for other people to understand your code, I would suggest following some basic coding styles.

For example, the Oracle coding style is one of the popular coding styles for Java.

There are two types of comments in your coding style. The first is an implementation comment, which uses / * * / for block comments and // for comments on a single line.

/* * Here is a block comment. */ // Here is a single line comment. 

The second type is a documentation comment, which usually uses the comments / ** * / style and appears only before class, function, and variable definitions. For instance:

  /** * Documentation for some class. */ public class someClass { /** * Documentation for the constructor. * @param someParam blah blah blah */ public someClass(int someParam) { ... } ... } 
+5
source

First bullet:

  /** comment */ 

This type of comments is for documentation. Source: http://journals.ecs.soton.ac.uk/java/tutorial/getStarted/application/comments.html

Just indicating this as it is different from other types of comments. However, you may be right about multi-line comments.

+2
source

The Java language specification states that there are two types of comments: "//" and "/ * ... * /".

http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html#3.7

This is a trick. But since / ** ... * / is used by JavaDoc tools to create JavaDocs, I would say that the first choice is not an acceptable answer.

+2
source

You have to put this in a java file and compile each of them, and then see which one gives you an error. You do not need to speculate on this to guess the answer.

-1
source

Source: https://habr.com/ru/post/1481038/


All Articles