35

I tried searching through the Oracle documentation for an explanation of what the @code java annotation does.

From a previous question, I realized that it has something to do with html, but I'm not sure exactly what...

Would it be correct to say that by default javadoc is parsed as HTML... But placing the @code annotation next to some javadoc text will indicate that it should be treated as code, and not parsed/rendered in the usual way? So for example:

    /**
    *This is how to declare an int variable {@code int var=1;}
    */

Would that be a proper example of its use?

JMS
  • 1,039
  • 4
  • 12
  • 20

2 Answers2

50

{@code ...} is a Javadoc tag that tells Javadoc that the text inside the braces is source code and should not be treated as HTML. Javadoc should also format the text in a code block differently than the other text. This is a similar concept to the "code sample" text that the editor for StackOverflow uses when you format a question or answer.

Javadocs are specially-formatted source code comments for class descriptions, constructors, and methods to help generate HTML documentation about source code. For example the Java API is fully documented using Javadocs for reading online or in an IDE. See the Java API Documentation Generator for details.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
dkatzel
  • 31,188
  • 3
  • 63
  • 67
-4

If you want to add JavaDocs for a method. Maybe you don't know what are Javadocs

enter image description here

So the thing in yellow is a Javadoc here If you want to add code for a method as a Javadoc then you can annotate it with @code for others using the method to see the method as an example. and why we need to use it because if we don't do then the formatting won't be proper

You can refer to following question if you want to check the changes in formatting that take place if you don't add the @code: Multiple line code example in Javadoc comment

k1eran
  • 4,492
  • 8
  • 50
  • 73
The_Lost_Avatar
  • 992
  • 5
  • 15
  • 35