Introduction to Compiling Java
-
A program is made up of one or more classes
-
A class contains one or more methods
-
A method contains program statements
-
A Java application always executes the main method
Programming Language
machine language
assembly language
high-level language
fourth-generation language
Each CPU has its own specific machine language. The other levels
were created to make programming easier
A program must be translated into machine language before it can be
executed on a particular type of CPU
This can be accomplished in several ways:
-
A compiler is a software tool which translates source code into a specific
target language
-
Often, that target language is the machine language for a particular CPU
type
-
The Java approach is somewhat different
-
The Java compiler translates Java source code into a special representation
called bytecode
-
Java bytecode is not the machine language for any traditional CPU
-
Another software tool, called an interpreter, translates bytecode into
machine language and executes it
-
Therefore the Java compiler is not tied to any particular machine
-
Java is considered to be architecture-neutral
Step in Java Process
-
Edit/Translate/Execute Process in General
-
Edit - Program source is stored in a file. Written, modified,
updated using an editor.
-
Translate - Program source is converted from its textual form
into a form that can be executed by a machine (i.e., machine code or machine
language)
-
Compile - translate from high-level programming language to machine code
all in one shot
-
Interpret - translate from high-level programming language to machine code
a statement at a time, throwing away machine code for the statement after
it has been executed
-
Execute - Machine code is executed on the machine
-
Edit/Translate/Execute Process in Java

-
Edit -
-
Each file contains a single class
-
Use comment header!
-
File name is X.java, where the name X is the name of the
class.
-
Translate - uses both compile and interpreter
-
javac X.java - Compile X.java into X.class
-
X.class is not machine code - it is called java byte code
-
X.class is executed by a Java interpreter
-
Many languages are compiled (since it is efficient)! Why this approach?
-
Portability, sharing
-
Embed byte code in WWW browsers
-
Execute -
-
Time
-
Different times in programming
-
program writing time - when program is being edited
-
compile time - when program is being translated
-
execute time - when program is being executed
-
Different times in Java programming
-
program writing time - comments written when writing code, ignored by compiler,
never executed
-
detect errors at compile time - spelling errors, typos
-
system.out.println ("foo");
-
Systemoutprintln ("foo");
-
system.out.println ("foo);
-
execute time - statements in wrong order, mistakes in strings
-
System.out.println ("Print this second"); System.outprintln ("Print this
first");
-
System.out.println ("Komputer Science is fun!");
TerminologyYOU SHOULD KNOW AND UNDERSTAND THE TERMINOLOGY DESCRIBED ON
PAGES 20-21.
Based on material by: Alan
Kaplan of Clemson Univesity
Revisions responsibility of Barbara
Boucher Owens
Last modified:29 Aug 2006