0% found this document useful (0 votes)
6 views

Annotations & Generics in Java, java notes

Java Annotations are metadata tags used with classes, interfaces, methods, or fields to provide additional information for the compiler and JVM. Built-in annotations include @Override, @SuppressWarnings, and @Deprecated, while custom annotations can be created and accessed using reflection. Java Generics, introduced in J2SE 5, enhances type safety by enforcing specific object types in collections, eliminating the need for type casting and enabling compile-time checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Annotations & Generics in Java, java notes

Java Annotations are metadata tags used with classes, interfaces, methods, or fields to provide additional information for the compiler and JVM. Built-in annotations include @Override, @SuppressWarnings, and @Deprecated, while custom annotations can be created and accessed using reflection. Java Generics, introduced in J2SE 5, enhances type safety by enforcing specific object types in collections, eliminating the need for type casting and enabling compile-time checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Annotation is a tag that represents the metadata i.e.

attached with class,


interface, methods or fields to indicate some additional information which can be
used by java compiler and JVM.

Built-In Java Annotations


There are several built-in annotations in Java. Some annotations are applied to
Java code and some to other annotations.

used in Java code

o @Override
o @SuppressWarnings
o @Deprecated

used in other annotations

o @Target
o @Retention
o @Inherited
o @Documented

@Override
@Override annotation assures that the subclass method is overriding the
parent class method. If it is not so, compile time error occurs.

Sometimes, we does the silly mistake such as spelling mistakes etc. So, it
is better to mark @Override annotation that provides assurity that method
is overridden.
@Deprecated
@Deprecated annoation marks that this method is deprecated so compiler
prints warning. It informs user that it may be removed in the future
versions. So, it is better not to use such methods.
Example of custom annotation: creating, applying and
accessing annotation

//Creating annotation

import java.lang.annotation.*;

import java.lang.reflect.*;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

@interface MyAnnotation{

int value();

//Applying annotation

class Hello{

@MyAnnotation(value=10)

public void sayHello(){System.out.println("hello annotation");}

//Accessing annotation

class TestCustomAnnotation1{

public static void main(String args[])throws Exception{

Hello h=new Hello();

Method m=h.getClass().getMethod("sayHello");

MyAnnotation manno=m.getAnnotation(MyAnnotation.class);

System.out.println("value is: "+manno.value());

}}
Generics in Java
The Java Generics programming is introduced in J2SE 5 to deal with
type-safe objects. It makes the code stable by detecting the bugs at
compile time.

Before generics, we can store any type of objects in the collection, i.e.,
non-generic. Now generics force the java programmer to store a specific
type of objects.

Advantage of Java Generics


There are mainly 3 advantages of generics. They are as follows:

1) Type-safety: We can hold only a single type of objects in generics. It


doesn?t allow to store other objects.

2) Type casting is not required: There is no need to typecast the object.


3) Compile-Time Checking: It is checked at compile time so problem will not
occur at runtime. The good programming strategy says it is far better to handle
the problem at compile time than runtime.
Type Parameters
The type parameters naming conventions are important to learn generics
thoroughly. The common type parameters are as follows:

1. T - Type
2. E - Element
3. K - Key
4. N - Number
5. V - Value

Generic Method
Like the generic class, we can create a generic method that can accept
any type of arguments. Here, the scope of arguments is limited to the
method where it is declared. It allows static as well as non-static methods.

You might also like