Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
get the code
more resources

Prepare for Java Language Changes
Could another J2SE release rekindle the debate over Java's increasing complexity?
by Daniel F. Savarese

Posted October 10, 2003

The Java language is due for another round of changes that will debut in the Java 2 Platform, Standard Edition (J2SE) 1.5 beta release expected by the end of 2003. The sometimes closed nature of the Java Community Process (JCP) can make it difficult to stay ahead of the curve on these changes. Recently, enough information has been made available to develop a clearer picture of the form many of the latest round of language changes will take. I've covered some of these features in the past in my articles "Generics in Java" (Java Pro, September 2001), "Java's Continuing Evolution" (Java Pro, November 2002), and "The Case for Conditional Compilation" (Java Pro, March 2003), but not all of them. Get ready for some surprises.

J2SE 1.4 made it difficult to stick to the story that Java is simpler than other languages. J2SE 1.5 will make it more difficult. Although a host of new API changes—along with a number of class file format changes—are in store, the changes you will notice immediately are those that affect the syntax of the Java language. J2SE 1.5 will include the results of both JSR-14 ("Add Generic Types To The Java Programming Language") and JSR-201 ("Extending the Java Programming Language with Enumerations, Autoboxing, Enhanced for loops and Static Import"). The titles of the JSR proposals summarize the new syntax changes you should expect. You can test-drive all of these changes with the early access release of the J2SE 1.5 prototype compiler, which is available for download (see Resources).

ADVERTISEMENT

Enumerations have been a much-desired addition to the Java language. They serve at least three, if not more, purposes in C-like languages. An enumerated type allows you to restrict the range of values a variable may assume. An enumerated type defines an ordering to the values a variable may assume. An enumerated type defines constant integral values that can be used for array indexing and other specialized purposes. Even though some of these purposes can be served by integers or even C macro definitions, enumerations provide type-safety, guaranteeing that a variable will not assume a disallowed value.

The new Java enumerations are full-fledged types that can contain methods and member variables as well as fulfill the various roles of C enums. In Listing 1, variables of type Volume may assume only the values Silent, Soft, Audible, Loud, and Eleven. The declaration of these values defines an ordering, where Silent is assigned an ordinal value of 0 (accessible with Silent.ordinal) and Eleven is assigned an ordinal value of 4. The ordinal values are integer constants and can be used to index into arrays and for other purposes. Arbitrary values can be associated with enum values by adding member variables to hold those values. For example:

public enum Volume {
   ounce(1), cup(8), pint(16),
      quart(32), gallon(128);
   private final int ounces;
   Volume(int ounces) {
      this.ounces = ounces;
   }
  public int ounces() {
   return ounces;
  }
}



Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home