Generic Programming
Created By: Aparnathi Bhagu
Generics in Java
The Java Generics programming is introduced in J2SE 5 to
deal with type-safe objects.
Before generics, we can store any type of objects in collection
i.e. non-generic. Now generics, forces the java programmer to
store specific type of objects.
Generics is a way of implementing generic programming.
Generic programming provides the facility like for a set of
related methods, declare a single method that support any
valid types. Generics are introduced in java 5. In java,
generics are added because of Compile time type safety.
Generics in Java
Let us understand with an example. We have an
ArrayList of string type and add an integer type
element. Before java 5 this code will compile and throw
exception at the runtime which is worse. Generics
provides the facility of compile time type safety i.e.
compiler check that the correct types should be used at
the correct places and no ClassCastException.
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.
Before Generics, we need to type cast.
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);//typecasting
Advantage of Java Generics
After Generics, we don't need to typecast the object.
List<String> list = new ArrayList<String>();
list.add("hello");
String s = list.get(0);
Advantage of Java Generics
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.
List<String> list = new ArrayList<String>();
list.add("hello");
list.add(32);//Compile Time Error
Syntax:
Syntax to use generic collection
ClassOrInterface<Type>
Example to use Generics in java
ArrayList<String>
Generic class
A class that can refer to any type is known as generic class. Here, we
are using T type parameter to create the generic class of specific type.
Let’s see the simple example to create and use the generic class.
Creating generic class:
class MyGen<T>{
T obj;
void add(T obj){this.obj=obj;}
T get(){return obj;}
}
The T type indicates that it can refer to any type (like String, Integer,
Employee etc.). The type you specify for the class, will be used to
store and retrieve the data.
Generic class
Let’s see the code to use the generic class.
class TestGenerics3{
public static void main(String args[]){
MyGen<Integer> m=new MyGen<Integer>();
m.add(2);
//m.add("vivek");//Compile time error
System.out.println(m.get());
}}
Output:2
Generic Method
Like generic class, we can create generic method that can
accept any type of argument.
Let’s see a simple example of java generic method to
print array elements. We are using here E to denote the
element.
Generic Method
public class TestGenerics4{
public static < E > void printArray(E[] elements) {
for ( E element : elements){
System.out.println(element );
}
System.out.println();
}
public static void main( String args[] ) {
Integer[] intArray = { 10, 20, 30, 40, 50 };
Character[] charArray = { 'J', 'A', 'V', 'A', 'T','P','O','I','N','T' };
System.out.println( "Printing Integer Array" );
printArray( intArray );
System.out.println( "Printing Character Array" );
printArray( charArray );
} }
Generic Method
Output: Printing Integer Array
10
20
30
40
50
Printing Character Array
J
A
V
A
T
P
O
I
N
T