i

JAVA Complete Course

Generics

Generics in Java is an opportunity for usage some generic types and functions. One of the common example of generics in Java is collections package. It is visible example of the usage of generics. For example, LinkedList is the parametrized type, where Double is the type parameter.

The main idea of generics is “type safety“. What does it mean? It’s just a guarantee by compiler that in case of correct type usage we won’t get any ClassCastException during runtime of application. One of the example is list of String  i.e. List. If you declare a list in java like List, then java guarantees that it will find out and complain whenever there will be any try to insert any non-string type into above list. The second important thing regarding generics is “type erasure“. It means that any information which is added during using generics won’t be present in bytecode. Inside bytecode, code will be same as it was before introducing generics in Java. This necessarily helps in generating and executing code written prior java 5 when generics were not added in language.

  • Generic methods are like templates, when we have only one method declaration but it can be called with different argument types. It is up to compiler to decide which type to use. Following are few features of generic methods: They have a type parameter (the diamond operator enclosing the type) in method signature just before return type.

  • Type parameters can be bounded (bounds are explained later in the article)

An example of defining a generic method to convert an array to a list:

It is possible in generic method to have more than one generic type. See an example below:

When we touched the topic of data types, we have mentioned that there are primitive data types and their wrapper classes. Generics doesn’t work with primitive data types, it can only accept object types as a generic. So writing List will give us compilation error.

What are annotations? Annotations are metadata. Here is another question: What is metadata? Metadata is data about data. So, annotations are metadata about code.

Why annotations were introduced in Java at all? Previously, before they were introduced, there was used XML for keeping metadata. People who were working with XML wanted something, which would be loosely coupled from code. The second important reason is that an annotation defines a standard way of defining metadata in code. Prior to annotations, people also were using their custom ways to define metadata. Annotations standardize the way for each developer to define their own metadata.

Java has few built-in annotations: @Override, @Deprecated, @SuppressWarning

Let’s now revisit how to create our custom annotation:

  • Annotations are created by using @interface, followed by annotation name as shown in the below example.

  • An annotation can have elements as well. They look like methods. For example in the code snipped below, we have four elements. We should not provide implementation for these elements.

  • All annotations extends java.lang.annotation.Annotation interface. Annotations cannot include any extends clause.