Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple line code example in Javadoc comment

Tags:

java

html

javadoc

I have a small code example I want to include in the Javadoc comment for a method.

/**  * -- ex: looping through List of Map objects --  * <code>  * for (int i = 0; i < list.size(); i++) {  *      Map map = (Map)list.get(i);  *      System.out.println(map.get("wordID"));  *      System.out.println(map.get("word"));  * }  * </code>  *   * @param query - select statement  * @return List of Map objects  */ 

The problem is the code example shows up in the Javadoc with no line breaks making it hard to read.

-- ex: looping through List of Map objects -- for (int i = 0; i list.size(); i++) { Map map = (Map)list.get(i); System.out.println(map.get("wordID")); System.out.println(map.get("word")); }  Parameters query - - select statement  Returns: List of Map objects  

I guess I am wrong in assuming the code tag would handle line breaks. What is the best way to format code examples in Javadoc comments ?

like image 633
Mark Avatar asked Feb 12 '09 15:02

Mark


People also ask

What is the need for Javadoc multiline comments?

Multi-line comments or slash-star comments are called block comments. Java's block comments are used when more than one line of comments are written. Javadoc comments or documentation comments are inserted to describe classes, interfaces, fields, methods, or constructors.

How do you write a Javadoc style comment?

Writing Javadoc Comments In general, Javadoc comments are any multi-line comments (" /** ... */ ") that are placed before class, field, or method declarations. They must begin with a slash and two stars, and they can include special tags to describe characteristics like method parameters or return values.

What is the use of documentation comments * * */) in a Java program?

The compiler ignores everything from /* to */. The compiler ignores everything from // to the end of the line. This is a documentation comment and in general its called doc comment. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.


1 Answers

In addition to the already mentioned <pre> tags, you should also use the @code JavaDoc annotation, which will make life much easier when it comes to HTML entities issues (in particular with Generics), e.g.:

* <pre> * {@code * Set<String> s; * System.out.println(s); * } * </pre> 

Will give correct HTML output:

Set<String> s; System.out.println(s); 

While omitting the @code block (or using a <code> tag) will result in HTML like this:

Set s; System.out.println(s); 

For reference, a full list of tag descriptions available in Java SE 8 can be found here.

like image 132
Fabian Steeg Avatar answered Sep 22 '22 00:09

Fabian Steeg