THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

forward to take full advantage of the new features of the 5.0 release. The technical issues involved in
application evolution and migration can be complex, and many of the changes to the language and libraries for
the 5.0 release were designed with backward compatibility and migration in mind. A full coverage of the
technical issues, let alone the management and logistics issues involved, is beyond the scope of this book, but
we do want to highlight a couple of areas for your consideration.


The first section gives a brief overview of how the compiler and virtual machine can deal with different
versions of the language and runtime. Next, we look at some ways of dealing with the different dialects that
language changes produce, using assertions as an example. Finally, we look at some of the issues involving
the integration of generic and non-generic code.


A.1. Language, Library, and Virtual Machine Versions


Each new release of the Java platform potentially changes three things:


The language itself, through the addition of new keywords or extensions to the use of existing
keywords



  • The libraries: new types and/or new methods on existing types
    The virtual machine, and in particular the format of the compiled classes that the virtual machine
    expects



Not every new platform release changes all threein particular, changes to the class format occur rarely because
of the major impact such changes can have.


The compiler that comes with the Java Development Kit (JDK) tracks changes to the language and virtual
machine through the use of "source" and "target" options that can be passed to it. By supplying an appropriate
source and target pair you can compile code that will run on current or older versions of the virtual
machineassuming the code is compatible with the source version of the language chosen.


The different source versions and their approximate meaning are as follows:


1.1 The oldest recognized definition of the language, which included the original language definition
together with nested types and blank final variables. This source version is no longer supported by the
compiler in the 5.0 release.



  • 1.2 Introduced the strictfp modifier.

  • 1.3 Same as 1.2; this version number was added for consistency.

  • 1.4 Introduced assert.
    1.5 Introduced generics, enums, annotations, and extended for loop. Also uses StringBuilder
    rather than StringBuffer for string concatenation.



Similarly, the target versions are



  • 1.1 Compliant with the first edition of the Java Virtual Machine Specification (JVMS)
    1.2 Compliant with the second edition of the JVMS. The main change involved the way static member
    accesses were encoded in the compiled class and resolved at runtime.




  • 1.3 No known change

  • 1.4 No known change
    1.5 Compliant with the third edition of the JVMS. This enables support for enums, annotations, and
    some aspects of generics. It also modifies the way class literals are implemented.



Because some language changes require new library classes, a change in the source version may imply a
minimum target value. For example, a source of 1.4 requires a target of 1.4 because an earlier version of the
virtual machine won't have the classes or methods that support assertions. Not surprisingly, a source of 1.5

Free download pdf