Create Software Components 

Using Java Level 2



Course Info




Java Demos




   Lecture 2

Compiling & Interpreting

Computers are only capable of recognising and executing commands given in their own native processor instruction set.  They do not understand instructions written in high level language form.  This means the Java source code instructions we write when creating an program are a complete mystery to the computer.

We write code in a high level language for our convenience.  It is fairly easy to understand once we are familiar with the language.  However, since a computer does not understand high level language our source code must be translated into a form the computer is capable of understanding, 'machine code'  

The process of converting source code into machine code is called compilation.  In such a case, ALL of the source code is converted to machine code, ready to be used by the processor.

Interpreting is slightly different because the source code instructions are converted into machine code as they are needed.  On the fly if you like.

The following notes explain:-



Just-In-Time Compiler

Java File Types



    What does compiling do?

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.

Compiling in Java is a two-step process unlike a single step process for other programming languages.  Java is special because it compiles source code into bytecode first instead of platform-dependent machine code.  


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 on.  

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.  



    What 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 code.

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)


Just-In-Time Compiler

    What 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.


Java File Types

    Source Code File

When you write Java code you must save it in text format and ensure the file is saved with a .java file extension.  


Also, the name of your file must be EXACTLY the same as your class name.


    Bytecode File

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.

e.g.       myApp.class


If the program is an application with a main method, you can now execute your program.  At the command prompt you would type... 

c:>java  myApp

You should note that a file extension is not included in the command.  Which file is being executed?

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 file. 

What if your program is not an application but an applet?  In that case another file is required, a HTML file.


    HTML File

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.

The 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 utility.


    Summary of files you must create

When writing a Java application you will end up with two files.

.java     Your source code

.class    Your compiled bytecode

When writing a Java applet you will end up with three files.

.java     Your source code

.class    Your compiled bytecode

.html     Your HTML code



  • Computers don't understand source code..

  • Computers understand native machine code.

  • Native machine code will be platform-specific

  • Native machine code compiled for one particular platform will not run on another platform.

  • Normally, compiling converts source code into platform dependent machine code.

  • Java compiles 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 interpreting.

  • The JIT translates bytecode into native machine code for the platform it is running on.


That is folks!!



  Site Home 

Java Home   


Course Info





Scheme of Work





Learning Center

Web Materials


Java Demos



Lecture Materials

Tutorials & Notes





Site Home


Unit Home

ADR 2002