Once you have written
some Java source code, to run it on the computer you have to translate it into
machine code. Computers don't understand source code. They are only capable of recognizing
and executing commands given in their own native processor instruction
set. This is known as machine code. Normally, the
compilation process converts source code into equivalent machine code
instructions. This operation is called compilation and is carried
out by a compiler. The Java compiler is called Javac.
Java compiles code
slightly differently to other compilers, say Visual Basic or Borland C++
because it does not convert the source code straight into machine code.
Machine code is platform specific which means that code is compiled for a
specific processor or processor family. Code compiled to run on a Windows
platform will not run on a Macintosh, for example.
Any computer that runs
the Java bytecode must have the Java
Virtual Machine installed (JVM) since the JVM finishes the
compilation process and translates the actual bytecode
into machine code specific to the processor that it is running
All this means is that Java is platform
independent. You don't have to worry about which platform your code will
run on. You just write the code, get the Java compiler to convert into
bytecode and let the JVM on the user's computer handle the rest of the compilation/translation process
whenever a user wants to execute your code.
does interpreting do?
When a user wishes to run a Java
program, the JVM mostly translates the bytecode into machine code on a line
by line basis as the program is being executed. This is called interpreting.
The alternative would be to compile ALL the bytecode at once into machine
code before the start of program execution and then execute the machine
Interpreting is a slow process and
so there is a performance hit. A program written and compiled
directly into native machine code will run faster than a program that is
interpreted line by line.
There is one trick used to speed up
the interpreting process, the use of a just-in-time compiler (JIT)
does the JIT do?
The JIT compiler translates ALL the
bytecode into native machine code. This means the bytecode does not
have to translated on the fly by the Java Virtual Machine and the program
will run faster.
Now, although Java bytecode is
platform-independent, the JIT compiler is not. Since the JIT compiler
converts bytecode into specific platform-dependent native machine
code then you need the correct JIT version for each platform.
Generally, you don't need to concern
yourself with JIT compilers since most browsers like Internet Explorer and Netscape
Navigator come with them.
Source Code File
When you write Java code you must
save it in text format and ensure the file is saved with a .java
Also, the name of your file must be
EXACTLY the same as your class name.
When you compile your Java source
code into bytecode, the java compiler, Javac
creates a bytecode file with the same name as your source code file but
gives it a .class file extension.
If the program is an application with
a main method, you can now execute your
program. At the command prompt you would type...
You should note that a file
extension is not included in the command. Which file is being
The file that is being executed is
the .class file and your .java
source code file is completely irrelevant at this point. However,
every time you make changes to your .java
file you must recompile your source code and make a new updated .class
What if your program is not an
application but an applet? In that case another file is required, a
If you wish to run an applet you can
either use a browser or the appletviewer utility that comes with the
Java software development kit. In both cases you nee to create a HTML
file. Browsers can only read HTML files.
important line in the following HTML code is the line..
<APPLET CODE="myApp.class" WIDTH=250 HEIGHT=125> </APPLET>
<TITLE> My First Applet </TITLE>
<APPLET CODE="myApp.class" WIDTH=250 HEIGHT=125>
This is the line that tells the
browser that you wish to display an applet. When the browser comes
across the APPLET line it hands over the job of interpreting or compiling
the bytecode to the JVM or the JIT.
The same is true of the appletviewer
Summary of files you must create
When writing a Java application you
will end up with two files.
.java Your source code
When writing a Java applet you will
end up with three files.
.java Your source code
.html Your HTML code
don't understand source code..
understand native machine code.
code will be platform-specific
code compiled for one particular platform will not run on another
compiling converts source code into platform dependent machine code.
source code into bytecode.
Java source code
is saved in a .java file
Java bytecode is
held in a .class file
An extra HTML
file is required to run an applet in a browser or when using appletviewer.
The Java Virtual
Machine translates bytecode line by line into machine code which is called
translates bytecode into native machine code for the platform it is
That is folks!!