Thursday, August 15, 2013

java 1.5 new jeatures VarArgs

VarArgs:

Variable is declare inside the method’s argument.It is

called as Varargs.

Syntax:

return type method_name( return type…. Value).

Example:

public class JdkVarArgs

        {

public static void main(String args[])

display();

display("hai","Welcome");

        }

        public static void display(String... values)//VarArg Method

        {

            System.out.println("Am Display method ");

         for(String s:values)

{

System.out.print(s);

}

        }


To learn more on  java training in chennai or java tutorial





Output:

Am Display method

Am Display method

Hai Welcome

Static import using java

Static Import:


The programmer to access any static member of a

class directly.

Here no need class name or method name.If we

import like static java.lang.Math.*; means we can use any

mathematics function within a program.

Example:


import static java.lang.Math.*;

public class StaticImport {

public static void main(String args[])

{

System.out.println("Square value"+sqrt(64.0));

System.out.println("Power value"+ceil(-7));

}


learn more on static import java training in chennai or java tutorial

Output:

Square value8.0

Power value-7.0

Friday, August 9, 2013

AutoBoxing and unboxing in java 1.5 features


AutoBoxing and Unboxing in Java 1.5 version



                              Autometic convension of primitive data types to wrapper type is Autoboxing. The opposite operation is known as unboxing. 
                                In jdk 1.4 conversion between wrapper object and primitive type should be done manually.
                                Now in jdk 1.5 it becomes more easier to do the conversion using Autobboxing.

To learn more on autoboxing and unboxing  java training in chennai or java tutorial by candid

Example 1 Auto Boxing

import java.util.*;

public class AutoBoxExample {

    public static void main(String args[]) {
        int a = 50;
        Integer a2 = new Integer(a);// Boxing

        Integer a3 = 5;// Boxing

        System.out.println(a2 + " " + a3);
    }
}


Example 2 - unboxinf


class UnboxExample {
    public static void main(String args[]) {

        Integer a = new Integer(50);

        int i = a;

        System.out.println(i);
    }
}

Generic in Java 1.5 tutorial

 

Generic in java 1.5 tutorial


                      To create collection with type safe object generic is used, it can reduce unnecessary cast in our application , Avoid type cast may be caused by operational errors, this tutorial compares the difference between 1.4 and 1.5 version of jdk

Old Syntax:

ArrayList o=new arrayList();
o.add(new Integer(3));
o.add(new Integer(4));
int i=((Integer)(list.get(0))).parseInt();


New Syntax:

ArrayList<Integer> o=new ArrayList<Integer>();
o.add(new Integer(3));
o.add(new Integer(4));
int i=(list.get(0).parseInt();

To learn more on Generic you can choose Java Training  or Java Tutorial

Example:

public class JdkGeneric<T> {

    private T t;

    public void add(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }

    public static void main(String[] args) {
        JdkGeneric<Integer> integerBox = new JdkGeneric<Integer>();
        JdkGeneric<String> stringBox = new JdkGeneric<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

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