JavaRush /Java Blog /Random EN /Annotations. Part one, a little boring

Annotations. Part one, a little boring

Published in the Random EN group
First part. I wrote very briefly about annotations with the SOURCE and CLASS types. It’s worth reading so as not to get lost in the second part and to start “misunderstanding” a little more =) There will definitely be at least one word here that you know!
Annotations.  Part one, a little boring - 1
The first time I saw them in problems here, I somehow didn’t notice them. Well, Override is hanging out, it was written by IDEA, so that’s how it should be. Over time, I realized that everything is much deeper. While you're studying, annotations seem like something useless, but necessary. You don't know why they are doing it. I think I read a couple of articles, they said “how great it is that now we have annotations, everything has become so simple.” But I didn’t know how it was before, and I didn’t understand that it was easier now. Now I know and I want to tell you a little. There are 3 types of annotations (RetentionPolicy):
  • SOURCE – compiler annotations
  • CLASS – data from the annotation will be written to bytecode but will not be available during operation. They write that many annotations in the standard library use this type, and now they keep it because of backward compatibility. Used for very specific tasks.
  • Question and answer on StackOverflow
  • RUNTIME – the most popular, used while the code is running.
Since part of the article was taken up by the introduction, I will write here about SOURCE and CLASS annotations. Here are the abstracts I could find (thanks to problem 3607). I don’t write about runtime, there are too many of them and that’s not the topic of the article. SOURCE:
  • java/lang/annotation/Native.class;
  • java/lang/SuppressWarnings.class
  • javax/annotation/Generated.class
  • ,java/lang/Override.class
CLASS: I don't know why annotations with type CLASS are needed. I couldn't find documentation for the existing annotations, so I think we can just leave this baggage behind. But if you find it, please share. SOURCE annotations:
  1. Native – a variable under this annotation can refer to native code;

  2. SuppressWarnings – suppresses various compiler warnings;

  3. Generated – marks the source code that was generated;

  4. Override – checks for method override.
More details:
Annotations.  Part one, a little boring - 2
Native - never seen and never used. I think this is a rather rare annotation, because... they use it if they need to run code in another “native” language. I tried to find a clear reference to her, but I couldn't.
Annotations.  Part one, a little boring - 3
SuppressWarnings - often used in the form @SuppressWarnings("unchecked"). Used to suppress warnings that you are aware of. The example above suppresses warnings about casting unchecked types. Again, I have only encountered it in this form and use.
Annotations.  Part one, a little boring - 4
Generated - I came across it now when the task requires me to generate classes from xsd files. These 3 annotations are quite specific and are most likely not of interest to you at the moment. I'll describe the last one.
Annotations.  Part one, a little boring - 5
Override - you use it all the time and it does a very useful thing. It's easy to make a mistake when overriding a method, unless IDEA does it. There are typos or just mistakes. This annotation will ensure that the method in the parent class is the same as our (labeled) method. This guarantees us that the method will be overridden and not added to. When refactoring the code, the method can be removed or changed. Again, the annotation will indicate the error to you. Without it, our method would simply be completed.
Annotations.  Part one, a little boring - 6
Boring? I would say yes, there is not much useful to take away from this article. Almost everything in it (90%) is a story about something that you will not use, or you will, but very rarely. The remaining 10% is hello and a description of the Override annotation, which at first glance is useless. Well, I think the second part of the article will be more interesting. There will be RUNTIME annotations, and they interact with the code in real time and create black magic. Annotations. Part two. Lombok.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION