Compile-time type safety for collections without casting
A generic is a method that is recompiled with different types as the need arises
Instead of saying List list = new ArrayList(); ,
you will have to say List<String> list = new ArrayList<String>();
a) Provides compile-time checking to make sure you are using the correct type
b) No casting
Eliminates the drudgery and error-proneness of iterators
Oldway:
for (Iterator i = c.iterator(); i.hasNext(); ) {
SomeTask tt = (SomeTask) i.next();
tt.cancel();
}
// c is of Collection Type
Newway:
for ( SomeTask task : c )
{
task.cancel();
}
Avoids manual conversion between primitive types (such as int) and wrapper types (such as Integer)
Java wont let you use a primitive type where an object is required, you need a wrapper. Similarly
vice-versa, you need to unwrap it.
But Java 1.5 makes it automatic.
For ex:
Map<String, Integer> map = new TreeMap<String, Integer>();
for (String str : list) {
map.put(str, 1);
}
Provides all the well-known benefits of the Typesafe Enum pattern
An enumeration, or “enum,” is simply a set of constants to represent various values
Lets you avoid qualifying static members with class names
Tools to generate boilerplate code from annotations in the source code
Leads to a "declarative" programming style where the programmer says what should be done and tools
emit the code to do it.