In this lesson you will create and compile your first Java program. Before you get started with this task there's something you should know about the
Java compiler[1] . Unlike compilers for other high-level programming languages, the Java compiler does not produce machine code for a particular type of computer. Instead, it produces machine code for the Java Virtual Machine (Java VM). Move your mouse cursor over the buttons below to see the fundamental differences between a Java compiler and a C compiler.
Comparing the Java Compiler to other Programming Language Compilers
The Java compiler is the program distributed with J2SE called javac, which translates Java source code (in .java files) intoRunnable bytecode (.class files). The compiler generates an architecture-neutral object file format, the compiled code is executable on many processors, given the presence of the Java run time system. The Java compiler does this by generating bytecode instructions which have nothing to do with a particular computer architecture. Rather, they are designed to be both easy to interpret on any machine and easily translated into native machine code on the fly.
This is not a new idea. More than twenty years ago, both Niklaus Wirth's original implementation of Pascal and the UCSD Pascal system used the same technique. With the use
of bytecodes, performance takes a major hit (but just-in-time compilation mitigates this in many cases). The designers of Java did an excellent job developing a bytecode instruction set that works well on today's most common computer architectures. And the codes have been designed to translate easily into actual machine instructions.
Unlike C and C++, there are no "implementation-dependent" aspects of the specification. The sizes of the primitive data types are specified, as is the
behavior of arithmetic on them. For example, an int in Java is always a 32-bit integer. In C/C++, int can mean a 16-bit integer, a 32-bit integer, or any other size that the compiler vendor likes. The only restriction is that the int type must have at least as many bytes as a short int and cannot have more bytes than a long int. Having a fixed size for number types eliminates a major porting headache. Binary data is stored and transmitted in a fixed format, eliminating the "big endian/little endian" confusion. Strings are saved in a standard Unicode format.
Java Libraries
The libraries that are a part of the system define portable interfaces. For example, there is an abstract Window class and implementations of it for
UNIX, Windows, and the Macintosh.
To compile a Java source code file, you will use the javac tool that's included with the Java SDK.
For example, to compile the Java source code in a file named HelloWorld.java you would issue the following command at the command prompt:
javac HelloWorld.java
Java Compiler - Exercise
Compile the HelloWorld program (Windows):
The output of this command would be a file named HelloWorld.class containing thebytecode[2] produced by the compiler. In the next lesson you will run your first Java program.
Java Compiler - Exercise