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.

Decimal system

Binary system

Conversion from decimal to binary and vice versa

Binary arithmetic

Octal system

Hexadecimal system

Parts of a Program

A program has the following parts. Some of the parts are optional.

Comments - this is text introduced in a program for documentation. The
computer ignores the comments.

Class - the name of the program is the name of a class in Java. The
body of the program is within braces {}.

Global variables - these are variables declared at the beginning of the
program and can be accessed by any method in the program.

Methods - these are sections of a program that have a specific
functionality and can be called several times during the execution of
the program. A method has a signature and a body. The signature has
the name of the method and the parameters that the method needs and the
value it returns. In the body you have local variables that can be
accessed only within the method.

Main Method - this is the method where the execution of the program
begins. The main method is called only once. It calls other methods
as needed.

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.

add 3 to register D1

store the contents of register D1 into memory location 16

jump to the instruction sequence starting at location 208
if register D3 is zero

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?

Simple - Java is similar to C and C++ but with fewer features. It
is cleaner and more elegant.

Object-Oriented - Java was designed to be an object-oriented
language from the start. It is easier to implement the object
oriented features in Java.

Distributed - Java is designed to support applications on
networks. Java supports various levels of network connectivity.
With Java it is as easy to open a remote file as it is to open
a local file.

Interpreted - The Java compiler generates byte-code for the
Java Virtual Machine rather than native machine code. The Java
interpreter than executes the byte code.

Robust - Java has eliminated certain features like pointers that
make for unsafe programming. It is strongly typed language. There
is an exception handling facility that allows the user to deal with
error conditions gracefully.

Secure - Java was designed for applications running over the Internet.
There is a security mechanism in place to make difficult to write
malicious code.

Architecture Neutral - The byte code that the Java compiler generates
can be executed on any platform that has the Java Virtual Machine.

Portable - Because the byte code can run on any platform, Java programs
are portable.

High-performance - Java does not have the same type of performance
as compiled C code. However, in interactive applications where
time is spent in user input, Java is on par with C programs.

Multithreaded - Java provides support for multiple threads of
execution that can handle different tasks.

Dynamic - Java can load classes as they are needed, even from
across a network as the program is running.

Your first Java program

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

javac HelloWorld.java

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 ClassName.java. JAVA IS
CASE SENSITIVE.