How can I use "<" and ">" in javadoc without formatting?
JavaJavadocJava Problem Overview
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?
Java Solutions
Solution 1 - Java
You can use <
for < and >
for > .
Solution 2 - Java
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
Solution 3 - Java
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.
Solution 4 - Java
Escape them as HTML: <
and >
Solution 5 - Java
You only need to use the HTML equivalent for one of the angle brackets. The <
can be represented as either <
or <
. Here's a sample taken from real Javadoc:
<pre> <complexType> <complexContent> <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> <sequence> [...]
This displays as:
<complexType>
<complexContent>
<restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
<sequence>
Solution 6 - Java
Interposition of <pre> and {@code} saves angle brackets and empty lines in javadocs and is widely used, see java.util.Stream for example.
<pre>{@code
A<B>C
D<E>F
}</pre>
Solution 7 - Java
Just surround it with {@code}
like this:
{@code <xmlElement>}
Solution 8 - Java
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}
Solution 9 - Java
In my case where I wanted to put in my javadocs List<SomeClass>
...
I added an even more specific information by giving the link to my SomeClass
, so here is my solution :
List<{@link SomeClass}>
Which resulted to a clean :
List<SomeClass>
With underlined 'SomeClass' directing to the specified class.
(of course if the SomeClass
is not in same package, the complete path should be referenced)