Writers' Community!
Home Page Two Columnists Q&A Submit an Article FAQs Contact Author Login
Article Submission
We Need YOUR Articles!
We'll Promote Them for FREE!

Author Login

New Authors
Register Here


Now Serving 7,784 Authors
70,504 Quality Articles
& 3,627 Current Users Online!
Featured Authors
WarpTalk (92)
Mogama (16,433)
Bruce Horst (138)
Joel Hendon (17,877)
Michael Ramzy (633)
E. Raymond Rock (3,064)
Ira Coffin (7,406)
Connor Davidson (5,137)
Ben Morrish (8,401)
Steve Kovacs (4,388)
Sandra E. Graham (8,072)
Fran Larson (2,158)
Shari Vaudo (418)
David Tanguay (9,593)

View All Featured Authors
Most Recent
UK Based C Programming Courses - Thoughts

Discussions on Networking Training Uncovered

Finding The Right CompTIA Training Compared

Computer Training for Microsoft Systems Considered

Finding The Right MCSA Course Uncovered

Where To Do Your Adobe Web Design Course Clarified

Microsoft SQL Computer Training Companies Described

Considering Cisco CCNA Retraining Insights

Home Based MCSE Training Explained

Adobe CS4 Design Training Around The UK - Thoughts

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
No Reader Ratings Available ?
Rate It  /  View Comments  /  View All Articles submitted by uCertify
Submitted Wednesday, May 23, 2007
uCertify (15)
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/



tweet this!



Reprint Rights

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

No comments yet.


Send a private message to uCertify about this article.
Was this article helpful to you? Leave a Public Comment or Question:

This Article has been viewed 30 times.
Article added to SearchWarp.com on 5/23/2007 1:42:36 AM.
View other articles written by uCertify (15)


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 Certification: Showdown At The Transport Layer... TCP vs. UDP !

How To Become A CCNA (Cisco Certified Network Associate)

Cisco Certification: Putting Together Your Own Home Lab

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

Cisco Certification: Troubleshooting and Debugging ISDN

Cisco CCNA / CCNP Certification: Deciphering PING Returns

Five Commands For Your Cisco CCNA/CCNP Home Practice Lab

Cisco CCNP Certification: An Introduction To ISIS

Viewed Live and Saved. Load Time: 0.172.

Home  |  Page Two  |  FAQ's  |  Contact  |  Terms of Service  |  Article Submission Guidelines  |  Questions & Answers  |  Privacy  |  Mission / About
Copyright © 1999-2009 SearchWarp.com, All Rights Reserved - SearchWarp.com is an IcoLogic, Inc. Company