FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Architecture & Design
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
March 27, 2003
No More Java Jokes

Rick Wayne
Channels, regular expressions and nonblocking I/O letJava catch up with other languages.
No More Java Jokes

At his SD West 2003 tutorial, “Java’s New I/O,” Jason Hunter claimed that the new I/O library in Java 2 Standard Edition is really designed for server-side developers. “The good thing about back-end programming,” added the author of Java Servlet Programming and the JDOM XML library, “is that your boss never comes in to tweak it.”

The new I/O—or “postmodern I/O,” as Hunter dubs it—may be hidden under the hood from end users, but it still has the potential to excite programmers. JDK 1.4 includes new capabilities and frees Java hackers from some ugly fetters. Among the new features cited by Hunter:

  • More-usable files, with throughput comparable with native-compiled languages;
  • Scalable network I/O, allowing thousands of connections without resorting to an explosion of threads;
  • Pluggable character sets; and
  • Regular expressions.

    Hunter sprinkled bits of Java trivia throughout his talk, noting the way a pretty woman spotted in a coffee shop got immortalized in every Java class file (each one starts with the hex sequence 0xCAFE 0xBABE).

    But along with the anecdotes, Hunter also delivered the goods on new features. Leading off with file and network I/O, he explained how the new Channel classes are more straightforward to work with. Since they use interfaces rather abstract classes, it’s considerably easier to incorporate Channel functionality in user or third-party code. They also provide new functionality. For one thing, Hunter explained, channels can be bidirectional: the same object can both read and write. He also expressed delight that Java finally provides file locking for the programmer—no more hand-coded lock files! And the new memory-mapped file objects are ideal for convenient access to data files, he said: Just hook up the channel to a Buffer object, and programmers can treat huge datasets as if they fit into memory. The Channel classes work hand-in-glove with the new Buffer classes, which handle type-specific data (IntBuffer, ByteBuffer and so on) in a clean fashion—programmers no longer need to translate everything in and out of byte arrays, and maintain their own state for reading and writing. Instead, Hunter described a simple interface for filling, emptying, and querying buffers that puts the busywork under the covers, where it belongs. And the stuff rips, too: Hunter quoted Sun’s Marc Reinhold, who led the Java Specification Request effort that led to the new I/O subsystem. Reinhold’s published benchmarks show the new I/O performing about twice as fast as the old classes.

    “C and C++ programmers used to laugh at our blocking I/O,” Hunter told the class. He explained that in order to deal with multiple clients, Java programs previously had to fire up separate threads of execution. For something like a busy Web server, he maintained, the thread-per-client model hit the wall quickly. But now the new Selector mechanism lets a single thread register interest in many I/O events, and responds to them as they occur.

    Hunter pointed out that Java programmers have felt the lack of regular-expression support for a long time, and that the Apache project scratched that itch with their “regexp” and “oro” libraries. But the new built-in regular expression support is simpler to use, in part because support for them is built into fundamental classes like String. “It’s nice to be Sun sometimes,” Hunter laughed. He explained the industrial-strength Pattern and Matcher classes that support precompiled regular expressions, case-insensitive matches and more. But the additions to the humble String class will probably affect more code. String now supports regular expression matching and replacing, removing a chronic headache for Java programmers. “How many times have you written code to do this?” he asked at one point, to a synchronized roomful of nodding heads. “Here again, I don’t have to write it, because Java does it for me. At some point, all Java programs will be one-liners—you’ll get it from either Sun or Jakarta and say “just do it.”

    TOP 5 ARTICLES
    No Top Articles.



  • MICROSITES
    FEATURED TOPIC

    ADDITIONAL TOPICS

    INFO-LINK