Comments and Documentation in Java
An annotation is metadata, or metatext, written about a main text rather than as part of it.
A Note Beside the Text
Think about a note you have seen beside a piece of writing. It might be a footnote explaining the background of a historical event, or a pencil mark in the margin of a textbook reminding you why a passage mattered. Neither note is part of the main writing, yet both exist to help you understand that writing. This article uses that everyday idea to explain how programs carry explanatory material alongside their code, and how Java extends that idea in a powerful way.
What do you think happens?
If you delete every comment from a Java program, what happens when the program runs?
Reveal answer
The program behaves the same.
Comments are a kind of annotation, and the compiler ignores them, so they have no effect on what the program does. Removing them changes how easy the code is to read, but not its behavior.
Main Text and Metatext
An annotation is metadata, or metatext, written about a main text rather than as part of it. The main text carries the content. The annotation carries information about that content, such as context, explanation, or a reminder for a reader.
The source uses the footnote in an old novel as an illustration. The footnote explains the historical background of an event the author mentioned, but it is not part of the story. Reading the story without the footnote still gives you the story. Reading the footnote gives you more understanding of it. The annotation sits beside the text, and it changes how you understand the text without changing the text itself.
Comments Are Annotations Too
Programs already contain a familiar form of annotation. A comment in a program is a kind of annotation: it is metadata written about the code, placed alongside it, and meant to explain what the code means to a human reader.
Because the compiler ignores comments, a comment has no effect on what the program does. Comments change how readable the program is for people, not how the program behaves.
Comments are not the only form of metadata a program can carry. The source makes this point directly: comments are only one type of metadata that might be added to programs. That raises a natural question. If comments are not the whole story, what else can be added to a program, and who decides what counts?
Tracing a Comment Through Compilation
Follow a single source file through the compiler. The file contains two kinds of material written about the code: ordinary comments and annotations. The diagram below shows what happens to each one. The compiler discards comments, so they do not reach the compiled output. Annotations pass through the compiler and are carried forward, which is what allows them to be used after compilation.
Read the diagram from top to bottom. Both comments and annotations start in the same source file. At the compiler, the comment path ends, because the compiler ignores it. The annotation path continues, so the compiled output still holds the annotation. This is the difference that matters: a comment is written for a human reader and is thrown away, while an annotation can be read by a program later.
What Java 5.0 Changed
Java 5.0 introduced a feature called annotations into the Java language. Its purpose was to make it easier to create new kinds of metadata for Java programs. Before this feature, the metadata a programmer could attach to a program was limited to what the language already offered. With annotations, the programmer can devise new ways of annotating a program.
| Question | Before Java 5.0 | With Java 5.0 annotations |
|---|---|---|
| Which kinds of metadata can be attached? | Only those the language already offered | Programmer can define new kinds of metadata |
| Who decides the kinds of annotation? | The language | The programmer |
| Can a program read the metadata? | Limited to what the language offered | Programs can read and use the annotations they define |
What changed when annotations were added, as described in the source
The reason programmers wanted this feature follows from that change. A fixed set of metadata types limits what a program can describe about itself. Annotations let a programmer invent a kind of metadata suited to the program's own needs and then write other programs that read it and act on it.
Reading Annotations From Another Program
A comment is written for a human reader and is discarded by the compiler. A Java annotation can be read by a program, and that program can act on what it finds. The source describes this with Javadoc comments: a documentation program processes them to create API documentation. The Java annotation feature generalizes that possibility, so programmers can define their own kinds of metadata for programs to read.
The sequence runs in order. The programmer compiles the source. The compiler keeps the annotation and drops ordinary comments. Later, a separate reading program looks into the compiled output, finds the annotation, and uses it. The source confirms the core idea that a program can read and use these annotations; the exact mechanics of how a reading program locates them are beyond what this article covers.
Comments Versus Annotations
The two kinds of material differ in who reads them and whether the compiler keeps them. The comparison below groups the features of an ordinary comment and of the Java annotation feature side by side.
Both are metadata written about a main text. The difference is the audience and the outcome. An ordinary comment serves only a human reader and has no effect on the program. A Java annotation can serve a human reader and also a program that reads it and acts on it.
Following a Javadoc Comment
A Javadoc comment from writing to documentation
A programmer writes a Javadoc comment describing a method, then compiles the program and runs it. What happens to the comment, and what can a documentation program do with it?
- 1Write the comment
The programmer places a Javadoc comment beside the method. It is metadata written about the code and meant to describe what the method is for.
- 2Compile the program
The compiler ignores the comment, so it does not change what the compiled program does.
- 3Run the program
Running the program produces the same behavior it would have produced without the comment, because comments have no effect on program meaning.
- 4Process with a documentation program
A separate documentation program reads the Javadoc comment and uses it to create API documentation.
The comment has no effect on running the program, yet it is still useful because a documentation program processes it to produce documentation.
Mistakes About Comments and Annotations
Common mistake Believing that deleting comments changes how the program behaves
Why it failsThe compiler ignores comments, so they have no effect on what the program does.
Do this insteadRemember that removing comments changes readability for people, not program behavior.
Common mistake Assuming an annotation is discarded the way an ordinary comment is
Why it failsAnnotations are carried forward past compilation and can be read by a program, while comments are ignored by the compiler.
Do this insteadTreat a comment as human-only metadata and an annotation as metadata a program can use.
Common mistake Thinking Java 5.0 annotations are only for people reading the code
Why it failsJava 5.0 annotations let programmers define new kinds of metadata that programs read and use.
Do this insteadThink of annotations as metadata that a program can act on, not only as explanatory notes.
Common mistake Thinking all comments are processed by a documentation program
Why it failsThe source names Javadoc comments as the example processed by a documentation program to create API documentation.
Do this insteadRecognize Javadoc as a specific case of annotation processing, while ordinary comments are for human readers.
Trying the Main Text and Metatext Split
Take a short method or class you have written, or imagine one. List three pieces of information that describe the code without being part of the code itself. For each one, decide whether it would be an ordinary comment for a human reader or a kind of metadata a program could read. Then explain in one sentence what the compiler does with each.
Hints
- Ask whether the information helps a person understand the code or whether a program would need to act on it.
- Remember that the compiler ignores comments and that annotations are carried forward.
Key Takeaways
- An annotation is metadata, or metatext, written about a main text rather than as part of it.
- Comments are a kind of annotation for human readers, and the compiler ignores them, so they have no effect on program behavior.
- Java 5.0 added annotations so that programmers can create new kinds of metadata and write programs that read and use them.
- During compilation, comments are discarded while annotations are carried forward, which lets another program read them later.