J2SE 5.0 language features

J2SE 5.0 language features

Generics
作用: communicate the element type of a collection to the compiler to avoid the unsafe downcasting.
和C++中parameterized types的作用相似 (在Thinking in java中Bruce Eckel提到过)。
Example: ArrayList<String> stringList =  new ArrayList<String>(); 
             ......Add elements to the stringList......
             String string1 = stringList.get(0);

For-each Loop
作用:if you need not operate the designated elements within the iterator or array, for-each loop will bring the beautiful code for you and reduce the error opportunities.
            eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays.
Example: String[] stringArray;
            for (String individualString: stringArray) {...}

I noticed that most of Russian guys in TopCoder use the for-each loop.

Autoboxing/Unboxing
作用:automates to box/unbox between primitive types and the appropriate wrapper types.
Example:  ArrayList<Integer> intList = new ArrayList<Integer>();
              int total = intList.get(0);

Typesafe Enums
作用:replace int Enum Pattern (e.g. public static final int XXX = 0;) to represent a fixed set of constants.
Example: public enum WEEKENDS { SAT, SUN };
             private final WEEKENDS weekend;

Varargs
作用:automates and hides the passing process in the case of the arbitrary number of parameters;
       eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists.
       Varargs can be used only in the final argument position.
Example: public static String format(String pattern, Object... arguments);

Static Import
作用:avoid qualifying static members with class names without the shortcomings of the "Constant Interface antipattern."
Once the static members have been imported, we don't have to use "ClassName.staticMemeber" in our classes.
Example: import static java.awt.Math.*; 

Metadata
作用:avoid writing boilerplate code by enabling tools to generate it from annotations in the source code.
Example: public @interface XXX {...}

More information about "J2SE 5.0 new features" could be found at java.sum.com.

你可能感兴趣的:(J2SE 5.0 language features)