Java Annotations | A Kick-start guide
Annotations are used heavily nowadays due to their simplicity and flexibility.
lets explore why we should use them to make our life easy.
- Introduction to annotations.
- Using some built in annotations.
- Creating our own annotation.
Well you must have heard this term annotations anywhere in java/android programming. Annotations were introduced in java5 they are like meta-data of your code.
I don’t want to confuse but according to oracle.
Annotations, are form of meta-data, that provide data about a program that is not part of the program itself
Lets start by answering this question:
What the heck on earth are java annotations..??
Well in simple words we can say that annotations are like some special Instructions which are provided to the compiler or JVM. Yes you heard this right you can talk to compiler, JVM give them orders to follow your instructions with the help of annotations.
Why we should use Annotations in our code..??
As we know that javacode is converted into bytecode by compiler so as to be understood by the JVM.
Annotations acts like an instructor to the compiler/JVM. these annotations can be used to provide a meaningful messages during compile time itself instead of ending up with a horrible runtime errors or during runtime it depends how we use.
- Like hey Compiler buddy just ignore warnings in my code having any specific category. (Compile time processing)
- Annotations are so popular that its used by many popular libraries like Junit, Spring, Hibernate etc during (Runtime processing)
- Its also used as a way to replace XML from your code which helps to maintain much cleaner code by generating Xml/code automatically for you. Which helps us stick to our main program logics as developers. (Code generation)
OK then How to use annotations in our code??
There is plethora of built in annotations which can be used most probably as per our needs but if in case there isn’t then we can create out own annotations.
So for using any annotation in java we need:
- Package/ Class/ method/ variable/ parameter which needs to be annotated and
- an annotation (Built in / custom).
Still Not getting anything ??? Don’t worry it’ll be more clear with Examples just go to my next article on using some very popular basic annotations like @Override, @SuppressWarnings, @Deprecated and there is lot’s more like creating our own custom annotation.
Hope this article gave you basic idea if yes do share and comment your views.