Structured Programming   «Prev  Next»
Lesson 5Java compiler
Objective Use the Java compiler.

Java Compiler versus C++ Compiler

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.

C++ versus Java Compilation
1) C++ versus Java Compilation

This shows how a C compiler on one computer would produce machine code specific to that architecture.
2) This shows how a C compiler on one computer would produce machine code specific to that architecture.
The image discusses the behavior of C compilers with respect to different computer architectures.
  • "Source file"
  • "Compiler"
  • "Executable"
  • "This shows how a C compiler on one computer would produce machine code specific to that architecture. For example, you would not be able to run machine code compiled on an Intel computer on a Macintosh computer."

This text contrasts with Java's cross-platform bytecode, emphasizing how C compilers generate machine code that is specific to an architecture, thus lacking the portability Java offers.

The output of the Java Compiler is referred to as Java bytecode.
3) The output of the Java Compiler is referred to as Java bytecode.
The image describes the Java compilation process and its output. Here's the text:
  • "Source file"
  • "The output of the Java compiler is referred to as Java bytecode. The Java compiler on an Intel computer will produce the exact same bytecode as the Java compiler of a Macintosh computer. This is very different from other high languages."
  • "Compiler"
  • "Executable file bytecode"
  • "Java Virtual Machine"
  • "Windows Linux"


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.

Java Virtual Machine
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

Compile Java Program - Exercise

Compile the HelloWorld program (Unix)
Compile Java Program - Exercise

[1]Java compiler: Translates Java source code into bytecode.
[2]Bytecode: Machine code for the Java Virtual Machine.

SEMrush Software