How can I use "<" and ">
" in javadoc without formatting?
You can use HTML entities <
and >
to escape the angle brackets <
and >
in Javadoc.
Example:
Under the hood: why escaping is necessary
Direct usage of <
and >
in Javadoc is not recommended as these are HTML tag indicators. To prevent misinterpretations, we use the magic of HTML escape codes <
and >
. These codes help <
and >
to gallivant freely in your Javadoc comments - no misinterpretations, no harsh judgement.
Translating Javadoc speak: {@code} vs. {@literal}
When documenting generic text or non-code elements, {@literal ...}
has got your back. On the other hand, {@code ...}
is your best friend when you need to illustrate code snippets. For example:
Multiline code in Javadoc: The <pre>{@code}</pre>
combo
For multilined code, mixing <pre>
tags with {@code}
is your ultimate winning move. The <pre>
tag helps maintain the formatting, while {@code}
ensures <
and >
can strut about incode style:
Deciding whether to use {@code} or {@literal}
- Use
{@code ...}
when your Javadoc docs feature XML snippets or other pieces of code. - Embrace
{@literal ...}
when your text includes characters like<
and>
, or if you want these characters to play the hide and seek game in the Javadoc.
Potential pitfalls and solutions
- Unescaped
<
and>
may result in compilation errors and broken Javadoc layouts. - Older releases of the Javadoc tool may not recognize
{@literal ...}
and{@code ...}
tags. - Always remember to check the official Java documentation for recent features and compatibility issues.
Was this article helpful?