"Hi, Amigo!"

"Hello, Rishi."

"Today I'll give you a deeper introduction to annotations."

"As you probably already know, annotations are special words that can be put next to classes, fields, methods, and variables."

"Yes. I encounter them very frequently."

"Sometimes they're also called metadata. Their primary purpose is to store specific additional information about methods, fields, and classes."

"Who do they store it for?"

"That's a very good question."

"People write annotations, which means that someone must need them."

"Annotations let you store additional information about the code and program elements, but, formally, they are not part of the code."

"Annotations can be used to generate XML, determine whether a method is deprecated, track bugs, etc."

"Here's an example of annotations in code:"

Example
@CatInfo(manager=Catmanager.class, unique=true)
class Cat
{
 @Name("Missy")
 private String name;

 @SuppressWarnings(value = "unchecked")
 void getUniqueCatName()
 {

 }
}

"As you can see, data can be stored in annotations."

"If an annotation has only one field called value, then the field name can be omitted:"

Example
@SuppressWarnings("unchecked")
void getUniqueCatName()
{

}

"If there are no parameters inside the parentheses, then they can also be omitted:"

Example
@Override
void getUniqueCatName()
{

}

"It's super easy to create your own annotation. Declaring an annotation is almost identical to declaring an interface."

Example
@interface CatManager
{
 Class manager();
 boolean unique();
 String name() default "Unknown Cat";
}

"There are only a couple of differences."

"First, you put an «@» sign before the word interface."

"Second, an annotation can have default values. You use the word default to set default values. See the example above. These parameters are optional and can be omitted when adding annotations."

"Ah. It's all easier than I thought. And I've been avoiding them like the Robo-Devil avoids holy water. It's not very pleasant when code has a lot of things you don't entirely understand."

"Oh, it's good that you reminded me — I want to tell you more about annotations used by the compiler."

"There are only 3 such annotations. Well, three so far."

@Deprecated.

"You can annotate a class or a method with @Deprecated. This will cause the compiler to issue a warning (a warning is not an error), and IntelliJ IDEA will display this method as strikethrough text. Something like this:

Example
Date date = new Date();
int year = date.getYear();

@Override.

"It's considered a best practice to add the @Override annotation to methods that you override."

"What's that for? Doesn't IDEA already show whether or not a method is overridden?"

"First off, there's IDEA and then there's Java syntax."

"And second, hypothetically, you could have a situation where a base class's method gets renamed without a corresponding change to the method name in the subclass. The program won't work as expected, but no one will notice. This annotation was invented to prevent these situations from happening:"

Example
@Override
void getUniqueCatName()
{

}

@SuppressWarnings.

"Sometimes the compiler displays a lot of warnings. And sometimes we know about the «problems» and are deliberately choosing to use the corresponding code that creates them. You can use this annotation to hide some of these warnings."

"A programmer can use the @SuppressWarnings annotation to tell the compiler, «Don't show a warning for this error — it's intentional.» For example:"

Example
@SuppressWarnings("unchecked")
void getUniqueCatName()
{

}

"Got it."

"I'm getting a little tired. I'm gonna go wet my parched throat. Let's continue after a break, OK?"

"Sure."