Header Ads

  • Breaking Now

    Interview Questions on Java 5 - Part II

    - What are Generic types and how it helps developers?
    Generic types are now part of J2SE 5.0. At the first instance one will see them in action is the Collections API. The Collections API provides common functionality like LinkedLists, ArrayLists and HashMaps that can be used by more than one Java type. The next example uses the 1.4.2 libraries and the default javac compile mode.

    ArrayList list = new ArrayList();
    list.add(0, new Long(10));
    int total = ((Integer)list.get(0)).intValue();

    The cast to Integer on the last line is an example of the typecasting issues that generic types aim to prevent. The issue is that the 1.4.2 Collection API uses the Object class to store the Collection objects, which means that it cannot pick up type mismatches at compile time. The first notification of a problem is a ClassCastException at runtime.

    The same example with the generified Collections library is written as follows:

    ArrayList list = new ArrayList();
    list.add(0, new Integer(42));
    int total = list.get(0).intValue();


    The user of a generified API has to simply declare the type used at compile type using the <> notation. No casts are needed and in this example trying to add a String object to an Integer typed collection would be caught at compile time.

    Hence Generic types enable an API designer to provide common functionality that can be used with multiple data types and which also can be checked for type safety at compile time.
    - What are Annotations and how are they used?

    Annotations have no direct impact on the execution of the code.They provide additional data about the program which is not part of the program itself.

    Annotations are used for:

    -Preferred over comments as documentation of a program is made quite flexible.
    -Program's declarations of classes, fields, methods, and other program elements.
    -Can be used by the compiler to detect errors or suppress warnings.
    -Compiler-time and deployment-time processing where annotation information can be processed to generate code, XML files, and so forth.
    —Some annotations are available to be examined at runtime.

    The annotation appears first, often (by convention) on its own line, and may include elements with named or unnamed values:

    @Author(
    name = "Victor Hugo",
    date = "9/20/2009"
    )
    class SampleClass() { }

    or

    @SuppressWarnings(value = "unchecked")
    void sampleMethod() { }
    If there is just one element named "value," then the name may be omitted, as in:
    @SuppressWarnings("unchecked")
    void sampleMethod() { }
    Also, if an annotation has no elements, the parentheses may be omitted, as in:
    @Override
    void mySuperMethod() { }


    - What is Enhanced for-loop feature?

    It is a popular feature introduced with in Java SE 5.0.
    There is always a backward compatibility with old style of for looping. The changed was of for-loop in Java 5 look something like the following:


    The newer form:
    for (String name : nameArray) {
    System.out.println("Name: " + name);
    }


    - What is static import feature in Java 5?
    - Enables imported static members to be referred as if class declares them within to be used. So no more need of qualifying static class name to access the methods defined in the class. Here is an example as given below:


    import static java.lang.Math.*;

    public class SampleStaticImport
    {
    public static void main( String args[] )
    {
    System.out.println(sqrt(625.0 ) );
    System.out.println(sin( 90.0 ) );
    }
    }



    - What are Varargs?

    The varargs functionality allows multiple arguments to be passed as parameters to methods. It requires the simple ... notation for the method that accepts the argument list and is used to implement the flexible number of arguments required for printf.They are most fondly used in core APIs including reflection, message formatting, and the new printf facility in Java 5. Read an article here on varargs for more details.

    Post Top Ad

    Post Bottom Ad