The Java Compiler
In this section, we’ll say a
few words about javac, the Java compiler that is
supplied as part of
Sun’s SDK. (If you are happily
working in another development environment, you may want to skip
ahead to the next section.) The javac compiler is
written entirely in Java, so it’s available for any platform
that supports the Java runtime system. The ability to support its own
development environments is an important stage in a language’s
development. Java makes this bootstrapping automatic by supplying a
ready-to-run compiler at the same cost as porting the interpreter.
javac turns Java
source code into a compiled class
that contains Java virtual machine byte-code. By convention, source
files are named with a .java extension; the
resulting class files have a
.class
extension. Each source code file is a
single compilation unit. As you’ll see in Chapter 6, classes in a given compilation unit share
certain features, such as package and
import statements.
javac allows you one
public class per file and insists the file have the same name as the
class. If the filename and class name don’t match,
javac issues a compilation error. A single file
can contain multiple classes, as long as only one of the classes is
public. Avoid packing many classes into a single source file.
Including non-public classes in a .java file is
one easy way to tightly couple such classes to a public class. But
you might also consider using inner classes (see Chapter 6).
Now for an example. Place ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access