Electronic Design

Java 5.0 Perks Up With New Language Constructs

Language enhancements galvanize Java 5.0 for the next era of embedded developments.

Java 5.0 promises exciting and significant changes to the Java community (see "Java 5.0 Enhancements," below). It will be a while, though, before version 5.0 features make their way to embedded developers. Among the expected changes are new virtual machine and compiler attributes that will require testing. Java is going into more real-time and safety-critical applications, so reliability of the underlying system is key.

Many features will pique developers, particularly improved internationalization support, inclusion of Java API for XML (JAXP), and an instrumentation library. This article concentrates on just the new language constructs. All Java developers will be able to take advantage of the new features, whereas certain upgraded features will be utilized by a subset of developers.

WHY CHANGE THE LANGUAGE?
For some time, Java has undergone little alteration to the language. Most improvements were made in the libraries, compiler, and virtual machine. This time, some major changes in the language will ultimately simplify programming.

The changes include generics, an enhanced loop construct, type safe enumerations, autoboxing, static imports, metadata, and variable-length argument lists. All were designed to minimize the impact on Java the language. Only one new keyword, enum, was added.

void removeZeroLengthStrings(Collection<String> c) \{
  for (Iterator<String> i = c.iterator(); i.hasNext(); )
    if (i.next().length() == 0)
       i. remove();

1. Generics are similar but simpler than C++ templates, where type parameters are noted by the angle brackets. It provides compile-time type checking.

boolean hasObject(Collection c, Object o) \{
  for (Object i : c)
    if(i.equals(o))
      return true;
  return false;

2. Shorter, safer, and clearer: The enhanced collection looping construct eliminates the need to explicitly define an iterator. The same syntax works for arrays as well.

public enum Coin \{
  PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
  Coin(int value) \{ this.value = value ;\}
  private final int value;
  public int value() \{ return value; \}

3. Type-safe enumeration can provide sophisticated, class-style extensions. In this case, the enumerated values (PENNY, etc.) can have a value associated with them.

public static String format 
  (String pattern, Object... argumentList)

4. Variable-length argument lists, a major feature missing from Java, can be very handy. The syntax allows the last parameter to be assigned an array that contains the remaining parameters passed to a method of this type. In this case, argumentList is type Object\[\].

The backwards-compatible changes do require updates, including the collection libraries, and make the new printf-style formatting possible. For more, see www.elecdesign.com.

Sun Microsystems
www.sun.com

See associated table

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish