TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Sunday, 12 April 2020

Generics In Java | Generics In Java Example | Generics In Java In Hindi

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.

generics in java example

Fig: Generics In Java 

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.
      List list = new ArrayList();    
      list.add(10);                         
      list.add("10");  
     With Generics, it is required to specify the type of object we need to store.
    List<Integer> list = new ArrayList<Integer>();    
    list.add(10);  
    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.
   List list = new ArrayList();    
    list.add("hello");    
    String s = (String) list.get(0);//typecasting    
    After Generics, we don't need to typecast the object.  
    List<String> list = new ArrayList<String>();    
    list.add("hello");    
    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.

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

Syntax

 To use generic collection
ClassOrInterface<Type>    

Example 

To use Generics in java
ArrayList<String> 

Example of Generic

    import java.util.*;  
    class TestGenerics2{  
    public static void main(String args[]){  
    Map<Integer,String> map=new HashMap<Integer,String>();     
    map.put(1,"vijay");   
    map.put(4,"umesh");  
    map.put(2,"ankit");  
    //Now use Map.Entry for Set and Iterator  
    Set<Map.Entry<Integer,String>> set=map.entrySet();  
I  terator<Map.Entry<Integer,String>> itr=set.iterator();  
   while(itr.hasNext()){  
   Map.Entry e=itr.next();//no need to typecast  
    System.out.println(e.getKey()+" "+e.getValue());   
}    }


     Output:

                             vijay
                        ankit
                        umesh


No comments:

Post a Comment