02-02-2013, 11:44 AM
Java Cookbook
1Java Cookbook.pdf (Size: 3.9 MB / Downloads: 43)
Introduction
This chapter covers some entry-level tasks that you simply need to know how to do before you
can go on -- it is said you must crawl before you can walk, and walk before you can ride a bicycle.
Before you can try out anything else in the book, you need to be able to compile and run your
Java, so I start there, showing several ways: the JDK way, the Mac way, and the Integrated
Development Environment (IDE) way. Then I'll discuss a few details about applets, in case you
are working on them. Deprecation warnings come next, as you're likely to meet them in
maintaining "old" Java code.
Command-line alternatives
Sun's javac compiler is the official reference implementation. But it is itself written in Java, and
hence must be interpreted at runtime. Recognizing the slowness of compilation as a significant
hindrance to developers, Sun's Java folk went back and rewrote the compiler from scratch,
discarding some old baggage and using new language features. This new compiler (still named
javac) was unveiled for early access in May 1999 and released later that year. It is about twice as
fast as the original Java compiler -- a big improvement -- but still slower than some other
compilers. Symantec's Java compiler and Microsoft's J++ (a Java-like language) are written in
C/C++, so they are quite a bit faster than an interpreted Java compiler.
In order to speed up my compilations, I have used Jikes, a freeware compiler written in C++.
Jikes is fast, free, and available both for MS-Windows and for Unix. It's also easy to install. For
MS-Windows (Win32), Linux, and other Unix systems, you can find binaries of the current version
on IBM's Jikes web site.
Discussion
It's less than an IDE (see the next recipe), but more than a command line. What is it? It's an
editor with Java support. Tools such as TextPad (http://www.textpad.com), Visual Slick Edit,
and others are low-cost windowed editors (primarily for MS-Windows) that have some amount of
Java recognition built in, and the ability to compile from within the editor. TextPad has quite a
number of file types that it recognizes, including batch files and shell scripts, C, C++, Java, JSP
(see Section 18.7), JavaScript (a client-side web technology), and many others. For each of
these, it uses color highlighting to show which part of the file being edited comprises keywords,
comments, quoted strings, and so on. This is very useful in spotting when part of your code has
been swallowed up by an unterminated /* comment or a missing quote. While this isn't the same
as the deep understanding of Java that a full IDE might possess, experience has shown that it
definitely aids programmer productivity. TextPad also has a "compile Java" command and a "run
external program" command. Both of these have the advantage of capturing the entire command
output into a window, which may be easier to scroll than a command-line window on some
platforms. On the other hand, you don't see the command results until the program terminates,
which can be most uncomfortable if your GUI application throws an exception before it puts up its
main window. Despite this minor drawback, TextPad is a very useful tool. Other editors that
include color highlighting include vim (an enhanced version of the Unix tool vi, available for MSWindows
and Unix platforms; see http://www.vim.org), the ever-popular Emacs editor, and
many others.