What are Generics?
Java Generics are a language feature that allows for definition and use of generic types and methods. Didn’t get it?
Java Generics are a language feature that allows for definition and use of generic types and methods. Didn’t get it?
Then Why Generics?
“In layman,s term, generics force type safety in java language and therefore add stability to your code by making more of your bugs detectable at compile time.” Thats the definition. Still not clear?
“In layman,s term, generics force type safety in java language and therefore add stability to your code by making more of your bugs detectable at compile time.” Thats the definition. Still not clear?
Ok Let’s see How it works!
Let’s assume you create an ArrayList that only works with Integers and you execute it with following values. As of following example it will give an error at runtime which is late to identify and error in a highly scaled dynamic application(I mean later when you are an expert ;))
Let’s assume you create an ArrayList that only works with Integers and you execute it with following values. As of following example it will give an error at runtime which is late to identify and error in a highly scaled dynamic application(I mean later when you are an expert ;))
List list = new ArrayList();
list.add(1000); //works fine
list.add("Gothama"); //run time error;
Let’s see how Generics solves the problem at compile time with following example.
List<Integer> list = new ArrayList<Integer>();
list.add(1000); //works fine
list.add("Gothama"); //compile time error;
What we do?
We simply add a type safety (i.e Generics) which will give and error at compile time which will save you lot of time. Ok, now you are clear.
We simply add a type safety (i.e Generics) which will give and error at compile time which will save you lot of time. Ok, now you are clear.
Is it applicable for methods too?
Yes, methods can use Generics too. See following example.
Yes, methods can use Generics too. See following example.
public class GenericsMethodTest{
public static void main(String args[]) {
// Create arrays of Integer, Double and Character
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
System.out.println("Array integerArray contains:");
printArray(intArray); // pass an Integer array
System.out.println("\nArray doubleArray contains:");
printArray(doubleArray); // pass a Double array
System.out.println("\nArray characterArray contains:");
printArray(charArray); // pass a Character array
}
// generic method printArray
public static < E > void printArray( E[] inputArray ) {
// Display array elements
for(E element : inputArray) {
System.out.printf("%s ", element);
}
System.out.println();
}
}
Output:
Array integerArray contains:
1 2 3 4 5
Array doubleArray contains:
1.1 2.2 3.3 4.4
Array characterArray contains:
H E L L O
Wow isn’t it amazing? Now you know what are Generics and when to use them right?
So how about Classes? Does Generics apply to classes too?
Yes, It applies to classes too. Check following example
Yes, It applies to classes too. Check following example
public class Box<T> {
private T t;
public void add(T t) {
this.t = t;
}
public T get() {
return t;
}
public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
Box<String> stringBox = new Box<String>();
integerBox.add(new Integer(10));
stringBox.add(new String("Hello World"));
System.out.printf("Integer Value :%d\n\n", integerBox.get());
System.out.printf("String Value :%s\n", stringBox.get());
}
}
Output:
Integer Value :10
String Value :Hello World
Now you know the basics of Generics…. Yay!