Friday, June 21, 2013

Java 5 New Features

Java Language New features in 1.5

Jdk 1.5 or Java 5 New features

  • Generics 

    Type safe collection is added in java version 5 to add compile-time safety
    Ex: in 1.4  collection should be created as
    ArrayList al=new ArrayList();
                     al.add("hai"); 
           al.add("welcome");
           al.add(new Integer(7));

     but in 1.5 version we can make the above ArrayList as safe Collection be adding generics 

      ArrayList<String> al=new ArrayList<String>();
       al.add("hai"); 
      al.add("welcome");
    Only String data can be added to the above arrayList

    If you try to add any data other than String it will throw an error.


    Java training will helps to learn more on Generics.

    If you are using Map Generics can be applied for both Key and Value

    Example:

    HashMap<String,Integer> hm=new HashMap<String,Integer>();
       hm.put("hai",new Integer(6));

     Note:

    If your upgrading your jdk version for old code , newer version will through an warning message to change it, but it wont affect your performance or output of your application.

    To know more on Generics online visit java tutorial


    Next post Enhanced for

No comments:

Post a Comment