Saturday, June 29, 2013

Auto Boxing in java 5



Auto Boxing

                              Auto boxing was the new features that was added newly in JDK 1,5 version
converting the primitive data type into wrapper object.

Friday, June 28, 2013

Java 5 Enhanced For Loop

Enhanced For Loop

 This enhanced For loop helps to iterate over an array or collection easily, it reduces the unwanted initialization , condition and increment operation for iterating a complete array or collection 
                  Example :

int[] a=new int[]{4,5,6,7,8,6,4,3};

for(int i=0;i<a.length;i++)
{
System.out.println(i);
}

can be written as (works only in 1.5 and greater version)

for(int i:a)
{
System.out.println(i)
         } 
get trained in new features of java from candid java training

 Enhanced for loop can be used only to iterate over a complete array or collection. we cannot give our own condition in it.

Example using Collections

             ArrayList al=new ArrayList();
                              al.add("hai");
                              al.add("welcome");                              al.add("ewew");

to iterate this ArrayList we can use enhanced for loop

             for(String s:al) 
            {
              System.out.println(s)
            }

Go through our online java tutorial

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