The Java Compiler
In this section, we’ll say a few words about
javac, the Java compiler in the JDK. The
javac compiler is written entirely in Java, so it’s
available for any platform that supports the Java runtime system.
javac turns Java source code into a compiled class
that contains Java bytecode. By convention, source files are named with a
.java extension; the resulting class
files have a .class extension. Each source code file
is considered 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 one public class per file and insists that 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 and is named for the file. Avoid packing too many classes into a single source file. Packing classes together in a .java file only superficially associates them. In Chapter 6, we’ll talk about inner classes, classes that contain other classes and interfaces.
As an example, place the following source code in the file BigBird.java:
packageanimals.birds;publicclassBigBirdextendsBird{...}
Next, compile it with:
%javacBigBird.java
Unlike the Java interpreter, which takes just a class name as its argument, javac needs a filename (with the .java extension) to process. The previous command produces the class file BigBird.class ...