Computer Programs

Aside: Computer Arithmetic

A computer manipulates binary digits (bits). A bit can be either 0 or 1. A byte is 8 bits. 1 KiloByte is 1024 bytes. 1 MegaByte is 1024 KiloBytes. 1 GigaByte is 1024 MegaBytes.

Parts of a Program

A program has the following parts. Some of the parts are optional. A program written in a high level language like Java has to be converted to machine language of the CPU that this program will run on.

Translating Programs to Machine Code

The CPU understands machine language. The machine language instructions are encoded as strings of bits. The instruction set is dependent on the type of CPU. The instructions are primitive like load, add, store, jump.

The machine instruction that adds 3 to register D1 might be encoded as the bit string:

0101 011 000000 001

Writing and understanding machine language code is error prone. The first step was to have short mnemonics for the primitive operations instead of binary strings. The above instruction could be written as:

ADD 3, D1

This is assembly language programming. An assembler translates the mnemonics to machine code. But even the assembly language code is unwieldy. The time to write, debug, understand, and maintain a piece of code is proportional to the number of lines of instruction.

A high level language allows common operations such as expression evaluation, repetition, assignment, and conditional action to be invoked in a single high-level statement. Examples of high level languages - Fortran, Pascal, C, C++, Lisp, SmallTalk, Basic, Java.

A computer does not understand a program that is written in a higher level language. A compiler translates a high level program into machine code for that particular CPU.

How does a computer run a program?

The program in machine language is first loaded into RAM. The computer reads the program one instruction at a time. As instructed the CPU reads data, manipulates it and stores it back. The CPU may also interact with the monitor, printer or speakers as instructed.

Why Java?

Your first Java program

public class HelloWorld
  public static void main ( String args[] )
    System.out.println ("Hello World!");

Your program has to be saved in a file called To compile the code for the Java Virtual Machine do:


This will produce a file called HelloWorld.class which is the byte code for the Java Virtual Machine. To run this code do:

java HelloWorld

Introduction to Java Programs

There are two types of Java programs - applications and applets. An application is a stand alone program whereas an applet is a program embedded in a web page that runs on the client machine.

The Java compiler compiles the source code of an application to the machine code of the Java Virtual Machine (JVM). The machine code of the JVM is known as byte-code. The byte code can be interpreted or it can be compiled by the JIT (just-in-time) compiler to machine code, or it can be run on computer chips whose machine language is byte-code.

A compiler translates the source code of a program into machine language of the computer. The compiler produces a file containing the optimized executable code. But this executable code is not platform independent, since different processors have different instruction set. Interpreters on the other hand directly parse and execute line by line the source code. The interpreter does not optimize the execution nor does it produce an executable file. Interpretation is platform independent but slow.

A program in Java consists of one or more class definitions, each of which has been compiled into its own .class file of the JVM byte code. One of these classes must define a method main() which is where the program starts running.

If a file contains a public class called ClassName then it must be saved in a text file called JAVA IS CASE SENSITIVE.