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