Home » Java » How can I use “<” and “>” in javadoc without formatting?

How can I use “<” and “>” in javadoc without formatting?

Posted by: admin November 30, 2017 Leave a comment

Questions:

If I write <xmlElement> in a javadoc, it does not appear, because tags have special functions on formatting texts.

How can I show this chars in a javadoc?

Answers:

You can use &lt; for < and &gt; for > .

Questions:
Answers:

Recent versions of JavaDoc support {@literal A<B>C}; this outputs the content correctly (escaping the ‘<‘ and ‘>’ in the generated HTML).

See http://download.oracle.com/javase/1.5.0/docs/guide/javadoc/whatsnew-1.5.0.html

Questions:
Answers:

Considering XML is actual code, I believe XML snippets in Javadoc are better suited for the {@code A<B>C} tag rather than the {@literal A<B>C} tag.

The {@code } tag uses a fixed-width font which makes its content standout as actual code.

Questions:
Answers:

Escape them as HTML: &lt; and &gt;

Questions:
Answers:

You only need to use the HTML equivalent for one of the angle brackets. The < can be represented as either &lt; or <. Here’s a sample taken from real Javadoc:

<pre>
&lt;complexType>
  &lt;complexContent>
    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
      &lt;sequence>
      [...]

This displays as:

<complexType>
   <complexContent>
     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
       <sequence>

Questions:
Answers:

If you set maven up to use markdown, you can just surround it with backticks.

`A<B>C` reads a bit nicer than {@code A<B>C}