Writers' Community!

Search:

Writers' Community!

SearchWarp Home Submit An Article Frequently Asked Questions Contact Author Login
Article Submission
We Need YOUR Articles!
We'll Promote Them for FREE!

Author Login

New Authors
Register Here


Now Serving 5,288 Authors
44,872 Quality Articles
& 5,174 Current Users Online!
Featured Authors
Missing Link (1,991)
Susan Thom (8,150)
Michelle Mackin (7,846)
David Tanguay (5,793)
Judge Dred (430)
Ben Jones (4,894)
Laura Trahan (30,541)
Joel Hendon (3,443)
Ieuan Dolby (1,283)
Jane Bullard (1,276)
Roschelle Nelson (688)
Dianne Lehmann (2,601)
Mike Fak (3,517)
Robert Melaccio, Sr. (4,435)

View All Featured Authors
Most Recent
CCNA Security Practice Exam: 10 Questions On The IOS Firewall Set

Cisco CCNP BSCI Practice Exam: 10 Questions On BGP

Cisco CCNA, CCNP, and CCENT Practice Exam: The Boot Process, WPA, Network Attacks, and More!

CCNA, CCNP, and CCENT Practice Exam Questions: Password Encryption, The IOS Firewall Set, And More!

CCNA, CCNP, And Cisco Security Practice Exam Questions: OSPF Stub Areas, Switches, And More!

CCNA, CCNP, and Cisco Security Questions: BGP Attributes, Switching Problems, And More!

CCNA, CCNP, And Security Exam Questions: Usernames, Passwords, TCP, And More!

CCNA, CCENT, And CCNP Practice Exam Questions: VTP, BGP Attributes, SRST, And More!

CCNA, CCNP, CCENT, And Cisco Security Questions: RFC Address Ranges, OSPF Router Types, And More!

CCNA, CCNP, and Cisco Security Training Questions: Configuring Multiple Interfaces And More!

Home » Categories » Computers & Networking » Technical Certification » New Features In Java Certification Exam. » Printer Friendly

New Features In Java Certification Exam.

Rated 3.5 out of 5
Rate It  /  View Comments  /  View All Articles submitted by uCertify
Submitted Wednesday, May 23, 2007
Submitted by: uCertify (24) Red Level Author Verified Account Contact uCertify
uCertify
Log in to become a member of uCertify's Fan Club!


Some of the important features added to Java 5.0 are as follows:
  • Autoboxing and Unboxing
  • Covariant returns
  • Enhanced for loop (for-each)
  • enum
  • Generics
  • Static Imports
  • Variable-argument lists (varargs)
  • StringBuilder class

Other additions to the exam, which are not new to Java are as follows:
  • Serialization
  • I/O Stream classes
  • NumberFormat, DateFormat, Locale
  • Pattern and Matcher classes, String.split() method
  • PrintWriter.format/printf methods
  • Comparable and Comparator interfaces
  • Arrays and Collections classes from java.util package
  • Setting the classpath correctly
  • OO concepts of coupling and cohesion

Autoboxing and Unboxing

It is not possible to put any primitive values, such as int or char, into a collection. Collections can hold only object references. To add any primitive to a collection, you need to explicitly box (or cast) it into an appropriate wrapper class. Again, while taking out an object out of the collection, it needs to be unboxed. To avoid these steps, Java 5.0 has added the autoboxing and unboxing features.

The example code shown below would have not compiled prior to version 1.5. But now that these new features have been added, it can be compiled without any difficulty.

class autoboxunbox
    public static void main(String[] args)
    {
        Character c = null;
        if (c == 'h')
        {
            System.out.println("I like boxing");
        }
        else
        {
            System.out.println("I like boxing very much");
    }
        }
}


The above example is simple to explain the concept of autoboxing and unboxing. However due to null assignment to c, during runtime, a NumberFormatException will be thrown in the code.

Covariant returns


Another new feature of Java 1.5 is that it supports for covariant return types. The use of covariant return types will allow a method overriding a super class method to return a subclass of the super class method's return type.

Consider the following lines of code:

class covarian{}
class subcovarian extends covarian{}
class supervarian
{
    covarian method1()
    {
        return new covarian();
    }
}
class SubClass extends supervarian
{
    subcovarian method1()
    {
        return new subcovarian();
    }
}


Had you tried to compile the above code using Java 1.4 or prior version, it would have definitely given an exception saying method method1 has already been defined in class supervarian. Covariant return types are just another feature that adds clarity and type safety to Java. Because of covariant return types, it becomes clear which method is being called. So, there is no cast required.


Generics


When anything is taken out of a collection, it needs casting. This is an annoying feature, when it is known what type of object was there in the collection. Generics help resolve this issue. Now, collections, such as ArrayList, can be bound to contain specific types of objects. Java's implementation of generics provides more compile-time type safety, which will enable the development of stronger and more self-describing APIs.

Consider the following example:

static void gener(Collection c)
{
    for (Iterator i = c.iterator(); i.hasNext(); )
    {
        String str = (String) i.next();
        if(str.length() == 10)
        i.remove();
    }
}


In the above example, although you know that the collection will hold String, even then you have to cast the retrieved element to string. Let us take a look at the same example using generics:

static void gener(Collection c)
{
    for (Iterator i = c.iterator(); i.hasNext(); )
    if (i.next().length() == 10)
    i.remove();
}


Generics make code more readable and avoid exception at runtime (classCastException), which may occur due to wrong casting, since the code is checked at compile time.


Enhanced for loop (for-each)


The for-each loop is a newly added feature to Java 5.0. The basic for loop was extended in Java 5 to make iteration over arrays and other collections more convenient. Let us take a very simple example with simple for loop, and then with the enhanced for loop.

Using simple for loop:

class foreachloop
{
    public static void main(String args[])
    {
       int[] arr={1,2,3,4,5,6,7,8,9,0};
        for(int i=0 ; i        {
            System.out.println(i);
        }
    }
}


Using the enhanced for loop:

class foreachloop
{
    public static void main(String args[])
    {
        int[] arr={1,2,3,4,5,6,7,8,9,0};
        for(int i : arr)
        {
            System.out.println(i);
        }
    }
}



Variable-argument lists


Although the feature of Variable-argument lists (varargs) is new to Java, it was used in C and C++. Previously, to invoke a method that takes arbitrary number of values, you needed to create an array and put the values into the array. Using varargs, you still pass the arguments in an array, but this is all done automatically. For example:

public varargseg method(String name, Class[] parameterTypes)

Now the same method is written as:

public varargseg method(String name, Class... parameterTypes)

Varargs can be used only in the final argument position. The dots after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.


Static import


Static import is a new feature introduced in J2SE 5.0. It allows static members to be treated as if they were declared in the class that uses them. Now, it is no longer needed to use the static class name and dot operator as was required before. Static imports are of two types, which are as follows:
  • One in which all the members of a particular static class are imported.
Syntax
import static packageName.ClassName.*;

  • And the other in which only a particular member of a class is imported.

Syntax
import static packageName.ClassName.membername;

For example:

import static java.lang.Math.*;
public class StaticImportTest
{
    public static void main( String args[] )
    {
        System.out.println( "Square root of 400 ="+sqrt(400.0 ) );
    }
}


StringBuilder


StringBuilder is a class, which is very similar to the StringBuffer class. It is a new concept introduced in J2SE 5. Like the String and the StringBuffer classes, it extends directly from the Object class. The StringBuilder class can act as an alternative to StringBuffer in places where the environment is not multithreaded, i.e, where synchronization or multithreading is not significant. The signature of the StringBuilder class is as follows:

public final class StringBuilder extends Object implements Serializable, CharSequence


Since StringBuilder is not synchronized, it offers faster performance than StringBuffer.

An Example of using StringBuilder is given below:

import java.util.*;
public class StrBld
{
    public static String likeJava(List list)
    {
        StringBuilder b = new StringBuilder();
        for (Iterator i = list.iterator(); i.hasNext(); )
        {
            b.append(i.next()).append(" ");
        }

        return b.toString();
    }

    public static void main(String[] args)
    {
        List list = new ArrayList();
        list.add("I");
        list.add("like");
        list.add("java");
        System.out.println(StrBld.likeJava(list));
    }
}

This will Compile and Execute to give the output "I like java"

Some of the important methods of StringBuilder class are as follows:

public StringBuilder append(char c)
It appends the string representation of the char argument to the current sequence.
The argument is appended to the contents of this sequence. It increases the length of the sequence by 1.

public int capacity()
This will return the current capacity of the StringBuider.

public StringBuilder insert(int offset,boolean b)
This will insert the string representation of the boolean argument into current sequence.

public StringBuilder reverse()
This will cause the character sequence to be replaced by the reverse of the sequence.

public String toString()

This will return a string representing the data in current sequence

public StringBuilder replace(int start, int end, String str)
This will replace the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists.

About the Author:

uCertify was formed in 1996 with an aim to offer high quality educational training software and services in the field of information technology to its customers. uCertify provides exam preparation solutions for the certification exams of Microsoft, CIW, CompTIA, Oracle, Sun and other leading IT vendors. To know more about uCertify, please visit http://www.ucertify.com/






Reprint Rights

Log in to become a member of uCertify's Fan Club!

Comments on this article:
No comments yet.


Was this article helpful to you? Leave a Public Comment or Question:

 

This Article has been viewed 24 times.
Article added to SearchWarp.com on Wednesday, May 23, 2007
View other articles written by uCertify (24) Red Level Author Verified Account Contact uCertify


If you found this article interesting, you may want to check out:

Disclaimer:  All information on this site is provided for informational purposes only! By no means is any information presented herein intended to substitute for the advice provided to you by any health care or other professional or organization.


Today's Most Popular
Cisco CCNA Certification Exam Tutorial: Route Summarization

Cisco CCNA Exam Tutorial: What's A Collision Domain?

Cisco CCNA / CCNP Home Lab Tutorial: Access Server Configuration

Cisco CCNA Exam Tutorial: Five OSPF Details You Must Know!

Cisco CCNA Exam Tutorial: The Best Time To Schedule Your Exam Is ....

Cisco Certification: The Definitive Guide To ARP, RARP, IARP, and Proxy ARP

CCNA / CCNP / BCMSN Exam Tutorial: VLAN Trunking Basics

Cisco CCNA Certification: Four Tips To Use DURING The Exam

Cisco CCNP Certification: An Introduction To ISIS

How To Become A Cisco Firewall Specialist

Home  |  FAQ's  |  Contact  |  Terms of Service  |  Article Submission Guidelines  |  Writers' Contests  |  Privacy  |  Mission / About
Copyright © 1999-2008 SearchWarp.com, All Rights Reserved - SearchWarp.com is an IcoLogic, Inc. Company