JDK 1.5 Language Enhancements
by krishna
Overview
- Generics
- Enhanced for loop
- Autoboxing and auto-unboxing
- Typesafe enumerated types (enums)
- Variable arguments (varargs)
- Static imports
- Metadata – Annotations
One of the main considerations in creating J2SE 5.0 was the ease of use for programmers. This slide lists some of the main changes to the language specification:
- Generics provides compile-time type checking and eliminate the need for casts. This serves to optimize collections of older Java versions.
- Enhanced for-loops make it easy to traverse through collections while avoiding error-prone iterators.
- Autoboxing and Auto-unboxing reduce the efforts previously required to cast primitive types back and forth. The compiler now does this for you.
- Typesafe enums provide a way to define enumerated types that can be checked by the compiler for type safety.
- Varargs allow a variable number of arguments to be passed to a method.
- Static Imports simplify the task of importing constants and other static members in your code like when you are importing packages.
- Metadata or annotations provide a standard way to annotate your code.
Generics
- Generics provide a way for you to communicate the type of a collection to the compiler so that it can be checked.
- The problem with using collections – Type safety:
- To take an element out of a Collection, you must cast it
- Besides being inconvenient, this is unsafe
- The compiler does not check that your cast is the same as the collection’s type
- The cast can fail at runtime
- Solution – Type-safe collections:
- With generics, you communicate the type of a collection to the compiler.
- The compiler can then check that you have used the collection consistently.
- When retrieving an element from the collection, the compiler performs the cast for you.
- Your code becomes cleaner and more reliable
Enhanced for Loop
- The for-each loop provides a simple syntax to iterate over collections and arrays:
for ( Type x: collection ) {
x.doSomething();
}
- Simplifies and speeds up the coding of iterators
- Preserves all of the type safety while removing the remaining clutter
Autoboxing and Auto-Unboxing
- You have to box and unbox primitive values (int, double, float, …) from the appropriate wrapper class (Integer, Double, Float, …) to put them into or get them out of collections.
- Before Java 5, the developer was responsible for the boxing and unboxing:
Integer grade = new Integer(10);
int g = grade.intValue();
- This required boxing and unboxing is tedious and clutters up your code
- With J2SE platform 5.0, the compiler automatically performs the boxing and unboxing:
Integer grade = 10;
int g = grade;
- The autoboxing and auto-unboxing feature automates the process, eliminating the clutter.
Typesafe Enums
- With the J2SE platform 5.0, the Java language provides typesafe enums.
- Java language typesafe enums resemble their C/C++ counterparts: enum Season { WINTER, SPRING, SUMMER, FALL }
- In addition, the new Java language enum declaration defines a full-fledged class – an enum type
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.
- Multiple arguments must be passed in an array, but the varargs feature automates and hides the process.
- Code sample: public static String format( String pattern, Object… arguments )
- In the preceding example, arguments parameter is an array of Objects.
Static Imports
- In order to access static members from other classes, it is necessary to qualify references with the class they came from: double r = Math.cos(Math.PI * theta);
- The static import allows unqualified access to static members
- Instead, the program imports the members, individually:
import static java.lang.Math.PI;
or in a group:
import static java.lang.Math.*;
- With static import, the previous code becomes:
double r = cos(PI * theta);
Metadata – Annotations
- Repeated writing of boiler plate code – For example, boiler plate code associated with using Java API for XML-based RPC (JAX RPC)
- The requirement for supporting side files – For example, BeanInfo files associated with JavaBeansTM architecture.
- The introduction of situation-specific annotation mechanisms – For example, the transient modifier in the Java platform.
- As of release 5.0, the J2SE platform has a general purpose annotation (also known as metadata)
- Now you can define and use your own annotation types
- The facility consists of:
- A syntax for declaring annotation types
- A syntax for annotating declarations
- APIs for reading annotations
- A class file representation for annotations
- An annotation processing tool (apt)
Overview Generics Enhanced for loop Autoboxing and auto-unboxing Typesafe enumerated types (enums) Variable arguments (varargs) Static imports Metadata – Annotations One of the main considerations in creating J2SE 5.0 was the ease of use for programmers. This slide lists some of the main changes to the language specification: Generics provides compile-time type checking and eliminate…
Recent Comments
Archives
- August 2025
- July 2025
- June 2025
- May 2025
- April 2025
- March 2025
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- February 2012
- January 2012
- December 2011
- October 2011
- August 2011
- July 2011
- May 2011
- January 2011
- November 2010
- October 2010
- September 2010
- July 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- August 2008
- July 2008
- June 2008
- December 2007
- April 2007
- January 2007