Programing

Java 주석의 / ** 및 / *

lottogame 2020. 5. 20. 07:47
반응형

Java 주석의 / ** 및 / *


차이점은 무엇입니까

/**
 * comment
 *
 *
 */

/*
 * 
 * comment
 *
 */

자바? 언제 사용해야합니까?


첫 번째 형태는 Javadoc 입니다. javadoc도구에서 생성 된 코드에 대한 공식 API를 작성할 때이 기능을 사용합니다 . 예를 들어, Java 7 API 페이지 는 Javadoc을 사용하며 해당 도구에 의해 생성되었습니다.

Javadoc에서 볼 수있는 몇 가지 일반적인 요소는 다음과 같습니다.

  • @param: 이것은 메소드에 전달되는 매개 변수와 예상되는 값을 나타내는 데 사용됩니다.

  • @return: 메소드가 어떤 결과를 돌려 줄지 나타내는 데 사용됩니다.

  • @throws: 특정 입력의 경우 메소드에서 예외 또는 오류가 발생 함을 표시하는 데 사용됩니다.

  • @since:이 클래스 또는 함수가 사용 가능한 가장 초기 Java 버전을 나타내는 데 사용됩니다.

예를 들어 다음과 같은 compare방법에 대한 Javadoc 이 있습니다 Integer.

/**
 * Compares two {@code int} values numerically.
 * The value returned is identical to what would be returned by:
 * <pre>
 *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
 * </pre>
 *
 * @param  x the first {@code int} to compare
 * @param  y the second {@code int} to compare
 * @return the value {@code 0} if {@code x == y};
 *         a value less than {@code 0} if {@code x < y}; and
 *         a value greater than {@code 0} if {@code x > y}
 * @since 1.7
 */
public static int compare(int x, int y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

두 번째 형식은 블록 (여러 줄) 주석입니다. 주석에 여러 줄을 추가하려는 경우이를 사용합니다.

나는 당신이 후자의 형태를 조금만 사용하고 싶다고 말할 것이다 . 즉, 메소드 / 복합 함수의 동작을 설명하지 않는 블록 주석으로 코드를 과도하게 사용하고 싶지 않습니다.

Javadoc은이 두 가지를 더 잘 설명하고이를 사용하여 실제 문서를 생성 할 수 있으므로 간단한 블록 주석보다 Javadoc을 사용하는 것이 더 좋습니다.


Java 프로그래밍 언어의 경우 두 언어 간에 차이없습니다 . Java에는 전통적인 주석 ( /* ... */)과 줄 끝 주석 ( ) 이라는 두 가지 유형의 주석이 // ...있습니다. Java 언어 사양을 참조하십시오 . 그래서, 자바 프로그래밍 언어, 모두 /* ... */와는 /** ... */즉, 기존의 코멘트 인스턴스이며, 그들은 모두 자바 컴파일러에 의해 정확히 동일하게 취급되어 무시됩니다 (: 그들은 공백으로 처리됩니다 이상 올바르게).

그러나 Java 프로그래머는 Java 컴파일러만을 사용하지 않습니다. 컴파일러, IDE, 빌드 시스템 등을 포함한 전체 툴 체인을 사용합니다. 이러한 툴 중 일부는 Java 컴파일러와 다르게 해석합니다. 특히 /** ... */주석은 Java 플랫폼에 포함되어 있고 문서를 생성 하는 Javadoc 도구로 해석됩니다 . Javadoc 도구는 Java 소스 파일을 스캔하고 그 사이의 부분 /** ... */을 문서로 해석합니다 .

이것은 like FIXME같은 태그와 유사합니다 TODO. // TODO: fix thisor 와 같은 주석을 포함하면 // FIXME: do that대부분의 IDE는 그러한 주석을 강조 표시하여 잊지 않도록합니다. 그러나 Java의 경우 주석 일뿐입니다.


첫 번째는 Javadoc 주석입니다. javadoc도구를 사용하여 클래스에 대한 API 문서를 생성 할 수 있습니다. 두 번째는 일반적인 블록 주석입니다.


JLS 3.7 섹션을 읽으면 Java 주석에 대해 알아야 할 모든 내용이 잘 설명되어 있습니다.

두 가지 종류의 주석이 있습니다.

  • / * 텍스트 * /

일반적인 주석 : ASCII 문자 / *에서 ASCII 문자 * /까지의 모든 텍스트는 무시됩니다 (C 및 C ++에서와 같이).

  • //본문

줄 끝 주석 : // ASCII 문자에서 줄 끝까지의 모든 텍스트는 무시됩니다 (C ++에서와 같이).


질문에 대해

첫번째

/**
 *
 */

Javadoc Technology 선언에 사용됩니다 .

Javadoc is a tool that parses the declarations and documentation comments in a set of source files and produces a set of HTML pages describing the classes, interfaces, constructors, methods, and fields. You can use a Javadoc doclet to customize Javadoc output. A doclet is a program written with the Doclet API that specifies the content and format of the output to be generated by the tool. You can write a doclet to generate any kind of text file output, such as HTML, SGML, XML, RTF, and MIF. Oracle provides a standard doclet for generating HTML-format API documentation. Doclets can also be used to perform special tasks not related to producing API documentation.

For more information on Doclet refer to the API.

The second one, as explained clearly in JLS, will ignore all the text between /* and */ thus is used to create multiline comments.


Some other things you might want to know about comments in Java

  • Comments do not nest.
  • /* and */ have no special meaning in comments that begin with //.
  • // has no special meaning in comments that begin with /* or /**.
  • The lexical grammar implies that comments do not occur within character literals (§3.10.4) or string literals (§3.10.5).

Thus, the following text is a single complete comment:

/* this comment /* // /** ends here: */

I don't think the existing answers adequately addressed this part of the question:

When should I use them?

If you're writing an API that will be published or reused within your organization, you should write comprehensive Javadoc comments for every public class, method, and field, as well as protected methods and fields of non-final classes. Javadoc should cover everything that cannot be conveyed by the method signature, such as preconditions, postconditions, valid arguments, runtime exceptions, internal calls, etc.

If you're writing an internal API (one that's used by different parts of the same program), Javadoc is arguably less important. But for the benefit of maintenance programmers, you should still write Javadoc for any method or field where the correct usage or meaning is not immediately obvious.

The "killer feature" of Javadoc is that it's closely integrated with Eclipse and other IDEs. A developer only needs to hover their mouse pointer over an identifier to learn everything they need to know about it. Constantly referring to the documentation becomes second nature for experienced Java developers, which improves the quality of their own code. If your API isn't documented with Javadoc, experienced developers will not want to use it.


Comments in the following listing of Java Code are the greyed out characters:

/** 
 * The HelloWorldApp class implements an application that
 * simply displays "Hello World!" to the standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); //Display the string.
    }
}

The Java language supports three kinds of comments:

/* text */

The compiler ignores everything from /* to */.

/** documentation */

This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.

// text

The compiler ignores everything from // to the end of the line.

Now regarding when you should be using them:

Use // text when you want to comment a single line of code.

Use /* text */ when you want to comment multiple lines of code.

Use /** documentation */ when you would want to add some info about the program that can be used for automatic generation of program documentation.


First one is for Javadoc you define on the top of classes, interfaces, methods etc. You can use Javadoc as the name suggest to document your code on what the class does or what method does etc and generate report on it.

Second one is code block comment. Say for example you have some code block which you do not want compiler to interpret then you use code block comment.

another one is // this you use on statement level to specify what the proceeding lines of codes are supposed to do.

There are some other also like //TODO, this will mark that you want to do something later on that place

//FIXME you can use when you have some temporary solution but you want to visit later and make it better.

Hope this helps


  • Single comment e.g.: //comment
  • Multi Line comment e.g: /* comment */
  • javadoc comment e.g: /** comment */

Java supports two types of comments:

  • /* multiline comment */ : The compiler ignores everything from /* to */. The comment can span over multiple lines.

  • // single line : The compiler ignores everything from // to the end of the line.

Some tool such as javadoc use a special multiline comment for their purpose. For example /** doc comment */ is a documentation comment used by javadoc when preparing the automatically generated documentation, but for Java it's a simple multiline comment.

참고URL : https://stackoverflow.com/questions/29815636/and-in-java-comments

반응형