Wednesday, August 26, 2020

Generics in Java

 

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.

Without Generics, we can store any type of objects.

  1. List list = new ArrayList();    
  2. list.add(10);  
  3. list.add("10");  
  4. With Generics, it is required to specify the type of object we need to store.  
  5. List<Integer> list = new ArrayList<Integer>();    
  6. list.add(10);  
  7. list.add("10");// compile-time error  

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

Before Generics, we need to type cast.

  1. List list = new ArrayList();    
  2. list.add("hello");    
  3. String s = (String) list.get(0);//typecasting    
  4. After Generics, we don't need to typecast the object.  
  5. List<String> list = new ArrayList<String>();    
  6. list.add("hello");    
  7. String s = list.get(0);    

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.

  1. List<String> list = new ArrayList<String>();    
  2. list.add("hello");    
  3. list.add(32);//Compile Time Error    

Syntax to use generic collection

  1. ClassOrInterface<Type>    

Example to use Generics in java

  1. ArrayList<String>    

Full Example of Generics in Java

Here, we are using the ArrayList class, but you can use any collection class such as ArrayList, LinkedList, HashSet, TreeSet, HashMap, Comparator etc.

  1. import java.util.*;  
  2. class TestGenerics1{  
  3. public static void main(String args[]){  
  4. ArrayList<String> list=new ArrayList<String>();  
  5. list.add("rahul");  
  6. list.add("jai");  
  7. //list.add(32);//compile time error  
  8.   
  9. String s=list.get(1);//type casting is not required  
  10. System.out.println("element is: "+s);  
  11.   
  12. Iterator<String> itr=list.iterator();  
  13. while(itr.hasNext()){  
  14. System.out.println(itr.next());  
  15. }  
  16. }  
  17. }  

  1. import java.util.*;  
  2. class TestGenerics1{  
  3. public static void main(String args[]){  
  4. ArrayList<String> list=new ArrayList<String>();  
  5. list.add("rahul");  
  6. list.add("jai");  
  7. //list.add(32);//compile time error  
  8.   
  9. String s=list.get(1);//type casting is not required  
  10. System.out.println("element is: "+s);  
  11.   
  12. Iterator<String> itr=list.iterator();  
  13. while(itr.hasNext()){  
  14. System.out.println(itr.next());  
  15. }  
  16. }  
  17. }  

Guitar Chords

click on   Guitar Chords Family