Since release 3.1, Eclipse includes full support for the new Java language features of J2SE 5.0. One of the most important consequences of this support is that you may not notice it at all--everything you expect to work for J2SE 1.4, including editing, compiling, debugging, quick fixes, refactorings, source actions, searching, etc., will work seamlessly with J2SE 5.0's new types and syntax. In this document, we will introduce some of the more interesting capabilities Eclipse users will find when working with J2SE 5.0.
Note that both version numbers '1.5' and '5.0' are used to identify the release of the Java 2 Platform Standard Edition. Version '5.0' is the product version, while '1.5' is the developer version and is also used for the compliance level.
In order to develop code compliant with J2SE 5.0, you will need a J2SE 5.0 or J2SE 6.0 Java Runtime Environment (JRE). If you start Eclipse for the first time using a J2SE 5.0 JRE, then it will use it by default. Otherwise, you will need to use the Java > Installed JREs preference page to register it with Eclipse.
This document introduces some of the new language features in J2SE 5.0 very briefly, but it is not a proper tutorial for these features. See here for more information.
To use the new J2SE 5.0 features, you must be working on a project that has a 1.5 compliance level enabled and has a 5.0 JRE. New projects will automatically get 1.5-compliance when choosing a 5.0 JRE on the first page of the New Java Project wizard:
To convert an existing J2SE 1.4 project to J2SE 5.0, you can simply:
Projects with different compliance levels can co-exist in the workspace, and depend on each other. You can also fine-tune the kinds of compile warnings and errors produced for each project using Properties > Java Compiler > Errors/Warnings. The Generic Types and the Annotations section contain options added for J2SE 5.0.
Generic types allow objects of the same class to safely operate on objects of different types.
For example, they allow compile-time assurances that a
List<String> always contains
Strings, and a
List<Integer> always contains
Invoking the refactoring produces:
Eclipse provides new options when searching for references to generic types. Consider this example:
Selecting the reference to
List<Integer>and using Search > References > Project from the context menu will highlight the List types on all four lines:
Using the Search View menu, the results can be filtered:
Filter Incompatible Type Arguments leaves only references to types that are assignment-compatible with the selected type:
Filter Inexact Type Arguments leaves only type references with the exact same signature:
Annotations attach metadata about how Java types and methods are used
and documented to the Java source and can then affect compilation or
be queried at run-time. For example,
@Override will trigger
a compile warning if the annotated method does not override a method in
Everything you can do with a Java type, you can do with an annotation:
A very useful annotation with full support in Eclipse is
For example, consider a private method that is currently unused, but you'd rather not delete:
Invoking quick fix on the warning proposes adding a
Selecting the quick fix adds the annotation. The Eclipse compiler honors the annotation by removing the warning on
Enumerations are types that are instantiated at runtime by a known, finite set of objects:
Again, anything you can do to a Java class can be done to an enumeration:
Autoboxing and auto unboxing allow for elegant syntax when primitive types are assigned to or retrieved from Object references:
Eclipse's source manipulation features handle autoboxing seamlessly, giving the correct types to new local variables and correct code assists. For code understanding, it is also possible to flag instances of autoboxing or autounboxing conversions:
For the common case of operating on each element of an array or collection in turn, J2SE 5.0 allows a new, cleaner syntax. Eclipse provides a "foreach" code template that can automatically guess the collection to be iterated:
Choosing the template produces:
Eclipse also provides a "Convert to enhanced for loop" quick-assist to
for loops where possible.
Static imports allow you to use static fields and methods from other classes without qualification.
Content assist in the Java editor can suggest such static members and adds a static import when required. To get such proposals, configure the static import favorites on the Java > Editor > Content Assist > Favorites preference page.
forloops to enhanced