Friday, February 6, 2009
J2SE 5.0 Features
Features of J2SE 5.0
Generics
Enhanced for Loop
Autoboxing/Unboxing
Typesafe Enums
Varargs
Static Import
Metadata (Annotations)
Generics
When you read 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.
Enhanced for Loop
It eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays.
Autoboxing/Unboxing
It allows you to put/retrieve primitive data types in the collection. e.g. You can't put an int (or other primitive value) into a collection.
Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is Integer in the case of int).
When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox the Integer using the intValue method.
All of this boxing and unboxing is a pain, and clutters up your code. The autoboxing and unboxing feature automates the process, eliminating the pain and the clutter.
Typesafe Enums
Enumerated type facility allows you to create enumerated types with arbitrary methods and fields.
It provides all the benefits of the Typesafe Enum pattern without the verbosity and the error-proneness.
Varags
This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists.
Static Import
The static import construct allows unqualified access to static members without inheriting from the type containing the static members.
Metadata (Annotations)
Metadata language feature lets you avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code.
Subscribe to:
Posts (Atom)