Sunday, April 15, 2012

Java 1.5 Feature : Varargs


In past releases, a method that took an arbitrary number of values required you to create an array and put the values into the array prior to invoking the method.

It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process. Furthermore, it is upward compatible with preexisting APIs.

Public static String format(String pattern,
Object... arguments);
The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position.

Much more simpler example here..

public class VarArgs {
public static void main(String[] args) {
String[] newArgs = {"a", "b", "c"};
vaMethod(newArgs);
}

public void vaMethod(String[] args) {
System.out.println("You gave me " + args.length + " args! Yay.");
}
}
You can declare it more easily, and not have to construct the array ahead of time:

public class VarArgs {
public static void main(String[] args) {
vaMethod("a", "b", "c");
}

public void vaMethod(String... args) {
System.out.println("You gave me " + args.length + " args! Yay.");
}
}


Java 1.5 Language Features


Generics

This long-awaited enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety. It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting. Refer to JSR 14.

Enhanced for Loop

This new language construct eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays. Refer to JSR 201 .

Autoboxing/Unboxing

This facility eliminates the drudgery of manual conversion between primitive types (such as int) and wrapper types (such as Integer). Refer to JSR 201 .

Typesafe Enums

This flexible object-oriented enumerated type facility allows you to create enumerated types with arbitrary methods and fields. It provides all the benefits of the Typesafe Enum pattern ("Effective Java," Item 21) without the verbosity and the error-proneness. Refer to JSR 201.

Varargs

This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists. Refer to JSR 201.

Static Import

This facility lets you avoid qualifying static members with class names without the shortcomings of the "Constant Interface antipattern." Refer to JSR 201.

Metadata (Annotations)

This language feature lets you avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code. This leads to a "declarative" programming style where the programmer says what should be done and tools emit the code to do it. Also it eliminates the need for maintaining "side files" that must be kept up to date with changes in source files. Instead the information can be maintained in the source file. Refer to JSR 175.


Wednesday, October 28, 2009

What is Generics in Java and how to use them


When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.

Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.
Here is a simple example taken from the existing Collections tutorial:


// Removes 4-letterwords from c. Elements must be strings
static voidexpurgate(Collection c) {
for (Iterator i = c.iterator();i.hasNext(); )
if (((String) i.next()).length() == 4)
i.remove();
}
Here is the same examplemodified to use generics:
static voidexpurgate(Collection<String> c) {
for(Iterator<String> i = c.iterator();i.hasNext(); )
if (i.next().length() == 4)
i.remove();
}
When you see the code <Type>,read it as “of Type”; the declaration above reads as “Collection of Stringc.” The code using generics is clearer and safer. We have eliminated an unsafecast and a number of extra parentheses. More importantly, we have moved part ofthe specification of the method from a comment to its signature, so thecompiler can verify at compile time that the type constraints are not violatedat run time. Because the program compiles without warnings, we can state withcertainty that it will not throw a ClassCastException at run time.The net effect of using generics, especially in large programs, is improved readability and robustness.
Generics has been added to the Java programming language in 2004 as part ofJ2SE 5.0.

As per JavaTM Language Specification
§ A type variable is an unqualified identifier. Typevariables are introduced by generic class declarations, generic interface declarations, generic method declarations, and by generic constructor declarations.
§ A class is generic if it declares one or moretype variables. These type variables are known as the type parameters of theclass. It defines one or more type variables that act as parameters. A genericclass declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized typesshare the same class at runtime.
§ An interface is generic if it declares one or moretype variables. These type variables are known as the type parameters of theinterface. It defines one or more type variables that act as parameters. Ageneric interface declaration defines a set of types, one for each possibleinvocation of the type parameter section. All parameterized types share thesame interface at runtime.
§ A method is generic if it declares one or moretype variables. These type variables are known as the formal type parameters ofthe method. The form of the formal type parameter list is identical to a typeparameter list of a class or interface.
§ A constructor can be declared as generic,independently of whether the class the constructor is declared in is itselfgeneric. A constructor is generic if it declares one or more type variables.These type variables are known as the formal type parameters of theconstructor. The form of the formal type parameter list is identical to a typeparameter list of a generic class or interface.

Simple Example
The following block of Java code illustrates a problem that existswhen not using generics. First, it declares an ArrayList oftype Object. Then, itadds a String to the ArrayList. Finally, it attempts to retrievethe added String and cast it to an Integer.
  List v = new ArrayList();
  v.add("test");
  Integer i = (Integer)v.get(0);
Although the code compiles without error, it throws a runtimeexception (java.lang.ClassCastException)when executing the third line of code. This type of problem can be avoided byusing generics and is the primary motivation for using generics.
Using generics, the above code fragment can be rewritten asfollows:
  List<String> v = new ArrayList<String>();
  v.add("test");
  Integer i = v.get(0); // (type error)
The type parameter String within the angle brackets declares the ArrayList tobe constituted of Strings (adescendant of the ArrayList's generic Object constituents).With generics, it is no longer necessary to cast the third line to anyparticular type, because the result of v.get(0) is defined as String bythe code generated by the compiler.
Compiling the third line of this fragment with J2SE 5.0 (or later)will yield a compile-time error because the compiler will detect that v.get(0) returns String insteadof Integer.
Wildcards
Generic type parameters in Java are not limited to specificclasses. Java allows the use of wildcards to specify bounds on the type ofparameters a given generic object may have. Wildcards are type parameters ofthe form "?", possibly annotated with a bound. Given that the exactelement type of an object with a wildcard is unknown, restrictions are placed onthe type of methods that may be called on the object.
As an example of an unbounded wildcard, List<?> indicates a list which has an unknownobject type.Methods whichtake such a list as an argument can take any type of list, regardless ofparameter type. Readingfrom the list will return objects of type Object, and writingnon-null elementsto the list is not allowed, since the parameter type is not known.
To specify the upper bound of a generic element, the extends keywordis used, which indicates that the generic type is a subtype of the bounding class.Thus it must either extend the class, or implement the interface of thebounding class. So List<? extends Number> means that the given list containsobjects of some unknown type which extends the Number class.For example, the list could be List<Float> orList<Number>. Reading an element fromthe list will return a Number, whilewriting non-null elements is once again not allowed.
The use of wildcards above are necessary since objects of one typeparameter cannot be converted to objects of another parameter. Neither List<Number> nor List<Float> is a subtype of the other (even thoughFloat is a subtype of Number). So, code that deals with List<Number> does not work with List<Float>. (If it did, it would bepossible to insert a Number that is not a Float intoit, which violates type safety.) The solution with wildcards works because itdisallows operations that would violate type safety.
To specify the lower bounding class of a generic element, the super keywordis used. This keyword indicates that the aforementioned generic type is asuper-type of said bounding class. So, List<? super Number> could represent List<Number> or List<Object>. Reading from a listdefined as List<? super Number> returns elements of type Object. Writing to such a list requires elementsof type Number or its subclasses.
Generic class definitions
Here is an example of a generic class:
public class Pair<T, S>
{
  public Pair(T f, S s)
  { 
    first = f;
    second = s;   
  }
 
  public T getFirst()
  {
    return first;
  }
 
  public S getSecond() 
  {
    return second;
  }
 
  public String toString()
  { 
    return "(" + first.toString() + ", " + second.toString() + ")"; 
  }
 
  private T first;
  private S second;
}
This generic class can be used in the following way:
Pair<String, String> grade440 = new Pair<String, String>("mike", "A");
Pair<String, Integer> marks440 = new Pair<String, Integer>("mike", 100);
System.out.println("grade:" + grade440.toString());
System.out.println("marks:" + marks440.toString());
Generic method definitions
Here is an example of a generic method using the generic classabove:
public <T> Pair<T,T> twice(T value)
{
   return new Pair<T,T>(value,value);
}
In many cases the user of the method need not indicate the typeparameters, as they can be inferred:
Pair<String, String> pair = twice("Hello");
The parameters can be explicitly added if needed:
Pair<String, String> pair = this.<String>twice("Hello");

Generics in throws clause
Although exceptions themselves cannot be generic, genericparameters can appear in a throws clause:
public <T extends Throwable> void throwMeConditional
      (boolean conditional, T exception) throws T
{
   if(conditional)
     throw exception;
}
Type erasure
Generics are checked at compile-time for type correctness. Thegeneric type information is then removed via a process called type erasure. For example, List<Integer> will be converted to the raw type (non-generic type) List, which can contain arbitrary objects.However, due to the compile-time check, the resulting code is guaranteed to betype correct, as long as the code generated no unchecked compiler warnings.
As a result, there is no way to tell at runtime which typeparameter is used on an object. For example, when an ArrayList isexamined at runtime, there is no general way to tell whether it was anArrayList<Integer> or an ArrayList<Float>. (There are partialapproaches — for example, individual elements may be examined to see what typethey belong to, since an ArrayList<Float> should never contain an Integer andvice versa; and it can be determined using reflection ifthe ArrayList actually belongs to anon-parameterized subtype of a specific ArrayList type, such as an ArrayListOfFloatsthat's declared to extend ArrayList<Float> — but no approach will work in allcases.)
The following code demonstrates that the Class objects appear thesame:
ArrayList<Integer> li = new ArrayList<Integer>();
ArrayList<Float> lf = new ArrayList<Float>();
if (li.getClass() == lf.getClass()) // evaluates to true
  System.out.println("Equal");
Java generics differ from C++ templates. Java generics generate onlyone compiled version of a generic class or function regardless of the number oftypes used. Furthermore, the Java run-time environment does not need to knowwhich parameterized type is used because the type information is validated atcompile-time and erased from the compiled code. Consequently, one cannotinstantiate a Java class of a parameterized type because instantiation requiresa call to a constructor, which is not possible when the type is unknown at bothcompile-time and runtime.
T instantiateElementType(List<T> arg)
{
  return new T(); //causes a compile error
}
Because there is only one copy of a generic class, staticvariables are shared among all the instances of the class, regardless of theirtype parameter. As a result, the type parameter cannot be used in thedeclaration of static variables or in static methods. Static variables andstatic methods are "outside" of the scope of the class'sparameterized types.