Java 5: New Feature’s Brief Overview : Interesting features...........

35 views
Skip to first unread message

Imran

unread,
Jul 11, 2007, 5:46:12 AM7/11/07
to technical-...@googlegroups.com
Hi All,
 
Here are few new features added by Java 5.
 
 
Last Major Release:     Java 1.2   - known as Java 2

Current Major Release:           Java 1.5 – known as Java 5

 

Following are briefs of new features added to Java 5 with examples –

 

Autoboxing – Makes like easy while working with primitive wrapper types.

 

Example (Java 2) –             int number = 13;

                                                Integer iNumber = new Integer(number);

 

Example (Java 5) –             int number = 23;

                                                Integer iNumber = number;

 

Conclusion: No need to wrap primitive types in corresponding primitive wrapper objects. This process of automatically boxing up the primitive type to primitive wrapper objects is known as "autoboxing".

 

Unboxing – reverse autoboxing.

 

Example (Java 2) - Boolean hasAttribute = baseVOImpl.getBooleanValue();

                                    if(hasAttribute.booleanValue())

                                                System.out.println("Its true value");

                                    else

                                                System.out.println("Its false value");

 

Example (Java 5) -  Boolean hasAttribute = baseVOImpl.getBooleanValue();

                                    if(hasAttribute)

                                                System.out.println("Its true value");

                                    else

                                                System.out.println("Its false value");

 

Conclusion: No need to convert primitive wrapper object to primitive type. Java5 will do unboxing for you.

 

Generics: feature that provides compile-time type safety for collections and also eliminates need of casting.

 

Example (Java2) – 

                                    // declare a list that contains BaseVOImpls

List grid = new ArrayList();

                                   

                                    // get 5th BaseVOImpl   from grid

                                    BaseVOImpl baseVO = (BaseVOImpl) grid.get(4);

Example (Java5) –

                                    // declare a list which contains BaseVOImpls

List<BaseVOImpl> grid = new ArrayList<BaseVOImpl>();

                                   

                                    // get 5th BaseVOImpl from grid

                                    BaseVOImpl baseVO = grid.get(4);

 

Conclusion: You will get compile time error if you try to add any object other than BaseVOImpl to grid. Also when you retrieve objects from grid, no need to cast them back to BaseVOImpl.

 

Another Example(Java2) -

                        public void someMethod(Collection names){

                                   

                                    for(Iterator itr = names.iterator();itr.hasNext();){

                                                String name = (String) itr.next();

                                                Displayer.display(name);

                                    }

                        }

 

Another Example(Java5) -

                        public void someMethod(Collection names){

                                   

                                    for(Iterator<String> itr = names.iterator();itr.hasNext();){

                                                String name =   itr.next();

                                                Displayer.display(name);

                                    }

                        }

 

Enhanced for loop: The enhanced for loop helps iterate over collections and arrays without using iterators or index variables.

Example (Java2) -

                        public void someMethod(Collection names){

                                    for(Iterator itr = names.iterator();itr.hasNext();){

                                                String name = (String) itr.next();

                                                Displayer.display(name);

                                    }

                        }

 

Example (Java5) -

                        public void someMethod(Collection names){

                                    for(Object nameObj : names){

                                                String name = (String) nameObj;

                                                Displayer.display(name);

                                    }

                        }

 

You can combine it with Generics and code will look like this –

                        public void someMethod(Collection<String> names){

                                    for(String name : names){

                                                Displayer.display(name);

                                    }

                        }

 

Static Import: In Java5, you can import static member of a class and refer to them without the usual class name prefix.

 

Example (Java2) -

                                    import   com.csc.fs.stp.cyberlife.constants.CoreValueConstants;

                                   

 

                                    if(tempValue == CoreValueConstants.ACCT_TYPE_DEBIT )

                                                ….

 

Example (Java5) -

 

import static com.csc.fs.stp.cyberlife.constants.CoreValueConstants.ACCT_TYPE_DEBIT

if(tempValue == ACCT_TYPE_DEBIT)

….                              

 

Not convince !!! See another Java5 example :

 

                        import static java.lang.System.out;

                       

                        out.println("Final Calculation Done.");

 

Note the use of "out" without "System" class prefix here.

 

Var-args: Variable arguments. In Java, to call a particular method of a class, you need to adhere to its signature. If there is a mismatch in number/type of arguments and no matching overloaded method is there, compiler will complain. But in Java5, now declare a method that takes variable number of arguments, something like JavaScript function.

Example (Java5) -

public void displayNames(String…args){

            for(String name : args){

                        System.out.println(name);

            }

 

}

 

Note three dots after String type in method signature. The above method can be called using variable number of "String" argument using array :

                                   

            displayNames(new String[]{"Jackie","Piralo"});

                        and     displayNames(new String[]{"Jackie","Piralo","Nadal"});

 

Note: The parameters must be "Object" and it must be the last or the only argument in the method signature.

 

Formatted Output (printf  method) : If you are familiar with "C" language then this is same as "printf()" function of "C".

Using this method, various format can be specified using "%" format specifier.

 

Example (Java5)

            System.out.println("Hello %2s and %1s","Pol","Tam");

 

Output: Hello Tam and Pol.

 

 % specifies that format specified is being used.

Number after % specified the number of the argument passed so %2 specify to use second parameter.

"s" specifies string.

 

Enumerated Types: Java5 lets you declare enumeration using "enum" keyword.

 

Example (Java2) –

           

class AccountType {

            static final CURRENT = 0;

            static final SAVING = 1;

}

But then you have to remember their int value 0 or 1.

 

Example (Java5) -

 

enum AccountType {

            CURRENT, SAVING

}

Now in your code you can code something like this :

class BankAccount {

 

            AccountType accType = AccountType.CURRENT;

           

}

 

Note "enum" is just a special type of class.

 

Scanners: No, no hardware talking. Scanner is a new addition to java util package. It simplifies reading of inputs from various character source including keyboard. Using this reading data from keyboard is as easy as writing "Hello World".

 

Example (Java5) -

                        Scanner input = Scanner.create( System.in);

                        System.out.println("Enter Name :");

                        String name = input.nextString();

                        System.out.println("Your name " + name);

 

Metadata (Annotation): This feature provides ability to associate additional data with Java classes/method/interface/fields. Compiler or tool processing your class can read this additional data. One of the powerful feature of   annotation is that based on configuration, this additional data can be stored in class file or can be discovered at runtime using Java reflection API.

 

One of the example of annotation is @Deprecated. When you this annotation, your class/method is marked as deprecated and compiler can generate "warning" about it.
 
 
Thanks & regards,
 
Imran
9811221852

neeraj bisht

unread,
Jul 11, 2007, 11:13:07 PM7/11/07
to technical-...@googlegroups.com
Great work !!!!!

On 7/11/07, Imran <imra...@gmail.com> wrote:
Hi All,
 
Here are few new features added by Java 5.
 
 
Last Major Release:     Java 1.2   - known as Java 2

Current Major Release:           Java 1.5 - known as Java 5

 

Following are briefs of new features added to Java 5 with examples -

 

Autoboxing - Makes like easy while working with primitive wrapper types.

 

Example (Java 2) -             int number = 13;

                                                Integer iNumber = new Integer(number);

 

Example (Java 5) -             int number = 23;

                                                Integer iNumber = number;

 

Conclusion: No need to wrap primitive types in corresponding primitive wrapper objects. This process of automatically boxing up the primitive type to primitive wrapper objects is known as "autoboxing".

 

Unboxing - reverse autoboxing.

 

Example (Java 2) - Boolean hasAttribute = baseVOImpl.getBooleanValue ();

                                    if(hasAttribute.booleanValue())

                                                System.out.println("Its true value");

                                    else

                                                System.out.println("Its false value");

 

Example (Java 5) -  Boolean hasAttribute = baseVOImpl.getBooleanValue();

                                    if(hasAttribute)

                                                System.out.println("Its true value");

                                    else

                                                System.out.println("Its false value");

 

Conclusion: No need to convert primitive wrapper object to primitive type. Java5 will do unboxing for you.

 

Generics: feature that provides compile-time type safety for collections and also eliminates need of casting.

 

Example (Java2) - 

                                    // declare a list that contains BaseVOImpls

List grid = new ArrayList();

                                    ...

                                    // get 5th BaseVOImpl   from grid

                                    BaseVOImpl baseVO = (BaseVOImpl) grid.get(4);

Example (Java5) -

                                    // declare a list which contains BaseVOImpls

List<BaseVOImpl> grid = new ArrayList<BaseVOImpl>();

                                    ...

                                    // get 5th BaseVOImpl from grid

                                    BaseVOImpl baseVO = grid.get(4);

 

Conclusion: You will get compile time error if you try to add any object other than BaseVOImpl to grid. Also when you retrieve objects from grid, no need to cast them back to BaseVOImpl.

 

Another Example(Java2) -

                        public void someMethod(Collection names){

                                   

                                    for(Iterator itr = names.iterator();itr.hasNext();){

                                                String name = (String) itr.next();

                                                Displayer.display(name);

                                    }

                        }

 

Another Example(Java5) -

                        public void someMethod(Collection names){

                                   

                                    for(Iterator<String> itr = names.iterator();itr.hasNext();){

                                                String name =   itr.next();

                                                Displayer.display(name);

                                    }

                        }

 

Enhanced for loop: The enhanced for loop helps iterate over collections and arrays without using iterators or index variables.

Example (Java2) -

                        public void someMethod(Collection names){

                                    for(Iterator itr = names.iterator();itr.hasNext();){

                                                String name = (String) itr.next();

                                                Displayer.display(name);

                                    }

                        }

 

Example (Java5) -

                        public void someMethod(Collection names){

                                    for(Object nameObj : names){

                                                String name = (String) nameObj;

                                                Displayer.display(name);

                                    }

                        }

 

You can combine it with Generics and code will look like this -

                        public void someMethod(Collection<String> names){

                                    for(String name : names){

                                                Displayer.display(name);

                                    }

                        }

 

Static Import: In Java5, you can import static member of a class and refer to them without the usual class name prefix.

 

Example (Java2) -

                                    import   com.csc.fs.stp.cyberlife.constants.CoreValueConstants;

                                    ...

 

                                    if(tempValue == CoreValueConstants.ACCT_TYPE_DEBIT )

                                                ....

 

Example (Java5) -

 

import static com.csc.fs.stp.cyberlife.constants.CoreValueConstants.ACCT_TYPE_DEBIT

...

if(tempValue == ACCT_TYPE_DEBIT)

....                              

 

Not convince !!! See another Java5 example :

 

                        import static java.lang.System.out;

                        ...

                        out.println("Final Calculation Done.");

 

Note the use of "out" without "System" class prefix here.

 

Var-args: Variable arguments. In Java, to call a particular method of a class, you need to adhere to its signature. If there is a mismatch in number/type of arguments and no matching overloaded method is there, compiler will complain. But in Java5, now declare a method that takes variable number of arguments, something like JavaScript function.

Example (Java5) -

public void displayNames(String...args){

            for(String name : args){

                        System.out.println(name);

            }

 

}

 

Note three dots after String type in method signature. The above method can be called using variable number of "String" argument using array :

                                   

            displayNames(new String[]{"Jackie","Piralo"});

                        and     displayNames(new String[]{"Jackie","Piralo","Nadal"});

 

Note: The parameters must be "Object" and it must be the last or the only argument in the method signature.

 

Formatted Output (printf  method) : If you are familiar with "C" language then this is same as "printf()" function of "C".

Using this method, various format can be specified using "%" format specifier.

 

Example (Java5)

            System.out.println("Hello %2s and %1s","Pol","Tam");

 

Output: Hello Tam and Pol.

 

 % specifies that format specified is being used.

Number after % specified the number of the argument passed so %2 specify to use second parameter.

"s" specifies string.

 

Enumerated Types: Java5 lets you declare enumeration using "enum" keyword.

 

Example (Java2) -

           

class AccountType {

            static final CURRENT = 0;

            static final SAVING = 1;

}

But then you have to remember their int value 0 or 1.

 

Example (Java5) -

 

enum AccountType {

            CURRENT, SAVING

}

Now in your code you can code something like this :

class BankAccount {

 

            AccountType accType = AccountType.CURRENT;

            ...

}

 

Note "enum" is just a special type of class.

 

Scanners: No, no hardware talking. Scanner is a new addition to java util package. It simplifies reading of inputs from various character source including keyboard. Using this reading data from keyboard is as easy as writing "Hello World".

 

Example (Java5) -

                        Scanner input = Scanner.create( System.in);

                        System.out.println("Enter Name :");

                        String name = input.nextString();

                        System.out.println("Your name " + name);

 

Metadata (Annotation): This feature provides ability to associate additional data with Java classes/method/interface/fields. Compiler or tool processing your class can read this additional data. One of the powerful feature of   annotation is that based on configuration, this additional data can be stored in class file or can be discovered at runtime using Java reflection API.

 

One of the example of annotation is @Deprecated. When you this annotation, your class/method is marked as deprecated and compiler can generate "warning" about it.
 
 
Thanks & regards,
 
Imran
9811221852

Daffodil Software Limited
2nd Floor, Vipul Orchid Plaza, Sun City, Sector 54,
Golf Course Road, Gurgoan, Haryana (India).
Tel: +91-124-414-5800
Fax: +91-124-414-5801
Mobile: +91-921-075-6743
E-Mail : neeraj...@daffodildb.com
WWW : http://www.daffodilsw.com
Reply all
Reply to author
Forward
0 new messages