215

How can I escape the @ symbol in javadoc? I am trying to use it inside a {@code} tag, which is inside <pre> tags.

I already tried the html escape &#64; sequence, but that didn't work.

1
  • Now it has started displaying fine after I adjusted some of the indentation inside the @{code} block! This was in Eclipse if it matters.
    – JayL
    Commented Feb 18, 2010 at 18:01

5 Answers 5

313
+50

Use the {@literal} javadoc tag:

/**
 * This is an "at" symbol: {@literal @}
 */

The javadoc for this will read:

This is an "at" symbol: @

Of course, this will work for any characters, and is the "officially supported" way of displaying any "special" characters.

It is also the most straighforward - you don't need to know the hex code of the character, and you can read what you've typed!

10
  • 2
    How do you escape the } symbol?
    – ADTC
    Commented Sep 6, 2013 at 9:01
  • 33
    I'm surprised this is accepted and so up-voted. Premise of the question: inside {@code} tag. {@literal} just doesn't work inside a {@code} tag. Commented Mar 28, 2016 at 19:12
  • 4
    What? The 11th of the month, '11 at 11:11? You should have posted this exactly a month earlier. ;-)
    – MC Emperor
    Commented Sep 14, 2017 at 10:42
  • 2
    @MCEmperor the java Calendar uses 0-based months, so this would be month #11 there - close enough?
    – Bohemian
    Commented Sep 14, 2017 at 17:55
  • 8
    Omitting the { and just using @literal @ works inside of a {@code} tag.
    – Brad Turek
    Commented Feb 10, 2018 at 22:35
71

Just write it as an HTML entity:

&#064;

From the document "javadoc - The Java API Documentation Generator"

If you want to start a line with the @ character and not have it be interpreted, use the HTML entity @.

This implies that you can use HTML entities for any character that you would need to escape, and indeed you can:

The text must be written in HTML with HTML entities and HTML tags. You can use whichever version of HTML your browser supports. The standard doclet generates HTML 3.2-compliant code elsewhere (outside of the documentation comments) with the inclusion of cascading style sheets and frames. HTML 4.0 is preferred for generated files because of the frame sets.

For example, entities for the less than symbol (<) and the greater than symbol (>) should be written as &lt; and &gt;. Similarly, the ampersand (&) should be written as &amp;.

1
  • 1
    It works better than the literal expression when not followed by a space (when writting annotation for example)
    – Bludwarf
    Commented May 25, 2016 at 14:56
12

my solution is

/**
 * Mapper Test Helper.
 *
 * add the following annotations above the class
 * <pre>{@code
 * // junit5
 * @literal @ExtendWith(SpringExtension.class)
 * // junit4
 * @literal @RunWith(SpringRunner.class)
 * }</pre>
 */
0
5

Fixed in javadoc tool from 15.0.2

This issue now appears to be fixed when using javadoc tool from 15.0.2.

That is, we no longer need to escape the @ character when using javadoc multiline {@code ... } block.

There is a JDK bug logged https://bugs.openjdk.java.net/browse/JDK-8130754 ... which is currently not marked as fixed but it no longer reproduces with javadoc 15.0.2.

2

You got the general idea, try using the octal representation: &#064;

1
  • @ has the codepoint 0x40 in hexadecimal, which is 64 in decimal Commented Dec 21, 2015 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.