Javadoc

Last updated

Javadoc is a documentation generator created by Sun Microsystems for the Java language (now owned by Oracle Corporation) for generating API documentation in HTML format from Java source code. The HTML format is used for adding the convenience of being able to hyperlink related documents together. [1]

Contents

The "doc comments" format [2] used by Javadoc is the de facto industry standard for documenting Java classes. Some IDEs, [3] like IntelliJ IDEA, NetBeans and Eclipse, automatically generate Javadoc templates. Many file editors assist the user in producing Javadoc source and use the Javadoc info as internal references for the programmer.

Javadoc also provides an API for creating doclets and taglets, which allows users to analyze the structure of a Java application. This is how JDiff can generate reports of what changed between two versions of an API.

Javadoc does not affect performance in Java as all comments are removed at compilation time. Writing comments and Javadoc is for better understanding the code and thus better maintaining it.

History

Javadoc was an early Java language documentation generator. [4] Prior to the use of documentation generators it was customary to use technical writers who would typically write only standalone documentation for the software, [5] but it was much harder to keep this documentation in sync with the software itself.

Javadoc has been used by Java since the first release, and is usually updated upon every new release of the Java Development Kit.

The @field syntax of Javadoc has been emulated by documentation systems for other languages, including the cross-language Doxygen, the JSDoc system for JavaScript, and Apple's HeaderDoc.

Technical architecture

Structure of a Javadoc comment

A Javadoc comment is set off from code by standard multi-line comment tags /* and */. The opening tag (called begin-comment delimiter), has an extra asterisk, as in /**.

  1. The first paragraph is a description of the method documented.
  2. Following the description are a varying number of descriptive tags, signifying:
    1. The parameters of the method (@param)
    2. What the method returns (@return)
    3. Any exceptions the method may throw (@throws)
    4. Other less-common tags such as @see (a "see also" tag)

Overview of Javadoc

The basic structure of writing document comments is to embed them inside /** ... */. The Javadoc comment block is positioned immediately above the items without any separating newline. Note that any import statements must precede the class declaration. The class declaration usually contains:

// import statements/** * @author      Firstname Lastname <address @ example.com> * @version     1.6                 (current version number of program) * @since       1.2          (the version of the package this class was first added to) */publicclassTest{// class body}

For methods there is (1) a short, concise, one line description to explain what the item does. This is followed by (2) a longer description that may span multiple paragraphs. The details can be explained in full here. This section is optional. Lastly, there is (3) a tag section to list the accepted input arguments and return values of the method. Note that all of the Javadoc is treated as HTML so the multiple paragraph sections are separated by a "<p>" paragraph break tag.

/** * Short one line description.                           (1) * <p> * Longer description. If there were any, it would be    (2) * here. * <p> * And even more explanations to follow in consecutive * paragraphs separated by HTML paragraph breaks. * * @param  variable Description text text text.          (3) * @return Description text text text. */publicintmethodName(...){// method body with a return statement}

Variables are documented similarly to methods with the exception that part (3) is omitted. Here the variable contains only the short description:

/** * Description of the variable here. */privateintdebug=0;

Note that it is not recommended [6] to define multiple variables in a single documentation comment. This is because Javadoc reads each variable and places them separately to the generated HTML page with the same documentation comment that is copied for all fields.

/** * The horizontal and vertical distances of point (x,y) */publicintx,y;// AVOID

Instead, it is recommended to write and document each variable separately:

/** * The horizontal distance of point. */publicintx;/** * The vertical distance of point. */publicinty;

Table of Javadoc tags

Some of the available Javadoc tags [7] are listed in the table below:

Tag & ParameterUsageApplies toSince
@authorJohn SmithDescribes an author.Class, Interface, Enum
{@docRoot}Represents the relative path to the generated document's root directory from any generated page.Class, Interface, Enum, Field, Method
@versionversionProvides software version information.Module, Package, Class, Interface, Enum
@sincesince-textDescribes when this functionality has first existed.Class, Interface, Enum, Field, Method
@seereferenceProvides a link to other element of documentation.Class, Interface, Enum, Field, Method
@paramname descriptionDescribes a method parameter.Method
@returndescriptionDescribes the return value.Method
@exceptionclassname description
@throwsclassname description
Describes an exception that may be thrown from this method.Method
@deprecateddescriptionDescribes an outdated method.Class, Interface, Enum, Field, Method
{@inheritDoc}Copies the description from the overridden method.Overriding Method1.4.0
{@linkreference}Link to other symbol.Class, Interface, Enum, Field, Method
{@linkplainreference}Identical to {@link}, except the link's label is displayed in plain text than code font.Class, Interface, Enum, Field, Method
{@value#STATIC_FIELD}Return the value of a static field.Static Field1.4.0
{@codeliteral}Formats literal text in the code font. It is equivalent to <code>{@literal}</code>.Class, Interface, Enum, Field, Method1.5.0
{@literalliteral}Denotes literal text. The enclosed text is interpreted as not containing HTML markup or nested javadoc tags.Class, Interface, Enum, Field, Method1.5.0
{@serialliteral}Used in the doc comment for a default serializable field.Field
{@serialDataliteral}Documents the data written by the writeObject( ) or writeExternal( ) methods.Field, Method
{@serialFieldliteral}Documents an ObjectStreamField component.Field

Examples

An example of Javadoc to document a method follows. Notice that spacing and number of characters in this example are as conventions state.

/** * Validates a chess move. * * <p>Use {@link #doMove(int fromFile, int fromRank, int toFile, int toRank)} to move a piece. * * @param fromFile file from which a piece is being moved * @param fromRank rank from which a piece is being moved * @param toFile   file to which a piece is being moved * @param toRank   rank to which a piece is being moved * @return            true if the move is valid, otherwise false * @since             1.0 */booleanisValidMove(intfromFile,intfromRank,inttoFile,inttoRank){// ...body}/** * Moves a chess piece. * * @see java.math.RoundingMode */voiddoMove(intfromFile,intfromRank,inttoFile,inttoRank){// ...body}

Doclets

Doclet programs work with the Javadoc tool to generate documentation from code written in Java. [8]

Doclets are written in the Java programming language and use the Doclet API to:

The StandardDoclet included with Javadoc generates API documentation as frame-based HTML files. Many non-standard doclets are available on the web [ citation needed ], often for free. These can be used to:

See also

Related Research Articles

<span class="mw-page-title-main">Java (programming language)</span> Object-oriented programming language

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need to recompile. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of the underlying computer architecture. The syntax of Java is similar to C and C++, but has fewer low-level facilities than either of them. The Java runtime provides dynamic capabilities that are typically not available in traditional compiled languages.

In computing, serialization is the process of translating a data structure or object state into a format that can be stored or transmitted and reconstructed later. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of object-oriented objects does not include any of their associated methods with which they were previously linked.

An HTML element is a type of HTML document component, one of several types of HTML nodes. The first used version of HTML was written by Tim Berners-Lee in 1993 and there have since been many versions of HTML. The current de facto standard is governed by the industry group WHATWG and is known as the HTML Living Standard.

<span class="mw-page-title-main">Doxygen</span> Free software for generating software documentation from source code

Doxygen is a documentation generator and static analysis tool for software source trees. When used as a documentation generator, Doxygen extracts information from specially-formatted comments within the code. When used for analysis, Doxygen uses its parse tree to generate diagrams and charts of the code structure. Doxygen can cross reference documentation and code, so that the reader of a document can easily refer to the actual code.

Plain Old Documentation (pod) is a lightweight markup language used to document the Perl programming language as well as Perl modules and programs.

JDiff is a Javadoc doclet which generates an HTML report of all the packages, classes, constructors, methods, and fields which have been removed, added or changed in any way, including their documentation, when two Java APIs are compared. Which can be used to described exactly what has changed between the two releases of a product. Only the API of each version is compared. It does not compare what the source code does when executed.

The event dispatching thread (EDT) is a background thread used in Java to process events from the Abstract Window Toolkit (AWT) graphical user interface event queue. It is an example of the generic concept of event-driven programming, that is popular in many other contexts than Java, for example, web browsers, or web servers.

Natural Docs is a multi-language documentation generator. It is written in C# and available as free software under the terms of the Affero General Public License. It attempts to keep the comments written in source code just as readable as the generated documentation. It is written and maintained by Greg Valure.

PHPDoc is an adaptation of Javadoc format for the PHP programming language. It is still an informal standard for commenting PHP code, but it is in the process of being formalized. It allows external document generators like phpDocumentor, which is the de facto standard implementation, to generate documentation of APIs and helps some IDEs such as Zend Studio, NetBeans, JetBrains PhpStorm, ActiveState Komodo Edit and IDE, PHPEdit and Aptana Studio to interpret variable types and other ambiguities in the loosely typed language and to provide improved code completion, type hinting and debugging.

XML Interface for Network Services (XINS) is an open-source technology for definition and implementation of internet applications, which enforces a specification-oriented approach.

The Java Modeling Language (JML) is a specification language for Java programs, using Hoare style pre- and postconditions and invariants, that follows the design by contract paradigm. Specifications are written as Java annotation comments to the source files, which hence can be compiled with any Java compiler.

Embedded Ruby is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML document, similar to ASP and JSP, and PHP and other server-side scripting languages. The templating system of eRuby combines Ruby code and plain text to provide flow control and variable substitution, thus making the combined code easier to maintain.

HeaderDoc is a documentation generator developed and maintained by Apple Inc. Using specially commented source code files as input, HeaderDoc generates documentation for the code in HTML or XML format. Syntax for HeaderDoc comment tags is largely similar to, and as of HeaderDoc version 8, supportive of Javadoc tags. HeaderDoc 8.7 and later also provides partial support for many Doxygen tags. Apple's HeaderDoc project is free, open source software distributed under the Apple Public Source License.

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

qooxdoo Open-source Ajax web application framework

qooxdoo is an open-source Ajax web application framework. It is an LGPL- and/or EPL-licensed client-side and server-agnostic solution, and includes support for professional JavaScript development, a graphical user interface (GUI) toolkit and high-level client-server communication.

<span class="mw-page-title-main">Comment (computer programming)</span> Explanatory note in the source code of a computer program

In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters. The syntax of comments in various programming languages varies considerably.

JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating. This is then processed, by various tools, to produce documentation in accessible formats like HTML and Rich Text Format. The JSDoc specification is released under CC BY-SA 3.0, while its companion documentation generator and parser library is free software under the Apache License 2.0.

Judoscript is a general purpose programming language designed primarily for scripting tasks on the Java platform. It was conceived and developed by James Jianbo Huang, starting in late 2001. Judoscript was one of the first so-called Java scripting languages; but its most striking characteristics is its audacious multi-domain support philosophy and practice.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

<span class="mw-page-title-main">Yesod (web framework)</span>

Yesod is a web framework based on the programming language Haskell for productive development of type-safe, representational state transfer (REST) model based, high performance web applications, developed by Michael Snoyman, et al. It is free and open-source software released under an MIT License.

References

  1. "Javadoc". agile.csc.ncsu.edu. Archived from the original on 13 June 2017. Retrieved 12 January 2022.
  2. "javadoc - The Java API Documentation Generator". Sun Microsystems . Retrieved 2011-09-30..
  3. IntelliJ IDEA, NetBeans Archived 2017-04-05 at the Wayback Machine and Eclipse
  4. "How to Write Doc Comments for the Javadoc Tool". Sun Microsystems . Retrieved 2011-09-30..
  5. Venners, Bill; Gosling, James; et al. (2003-07-08). "Visualizing with JavaDoc". artima.com. Retrieved 2013-01-19. When I did the original JavaDoc in the original compiler, even the people close around me pretty soundly criticized it. And it was interesting, because the usual criticism was: a good tech writer could do a lot better job than the JavaDoc does. And the answer is, well, yeah, but how many APIs are actually documented by good tech writers? And how many of them actually update their documentation often enough to be useful?
  6. "Java Platform, Standard Edition Tools Reference for Oracle JDK on Solaris, Linux, and OS X, Release 8. Section "Multiple-Field Declarations"" . Retrieved 20 Dec 2017.
  7. JavaSE 13 Documentation Comment Specification
  8. "Doclet Overview".