|
0. Introduction
Computerprograms are made up of machine language - zeroes and ones. Machine language is the
language that the computer system itself 'understands' and can carry out.
Machine language programs are written with
a program which is called an 'assembler'. Machine language and assembly
language are called low-level programming languages because they are close to the machine. Down here you'll see part of an assembler-program:
MOV AH, 01
INT 21
SUB AL, 30
CMP AL, 09
JLE 010C
SUB AL, 07
INT 20
You probably won't understand these lines - which is hardly
surprising: they are basically machine-instructions. But they still have to
be converted to machine language. A program that converts assembly language
into machine language does not have to be very complicated. Disadvantages of
programming in assembly language are that it is machine-dependent and it
takes a lot of work just to write even a very small program. So you'll
understand that assembly programming is not very popular.
The conversion of higher-level programming languages, such as Pascal or C, into
machine language is a lot more complicated. These languages are further
removed from machine language. Programs that are written in these high-level
languages clearly look more 'human', as you can see from the following part, in which the price
per unit is calculated depending on the number of
articles bought:
if (number >= 100)
pricePerUnit = 20;
else if (number >= 10)
pricePerUnit = 30;
else
pricePerUnit = 50;
(If you buy 100 or more, you pay 20. If you don't want to buy 100, but
are prepared to purchase at least 10, you'll pay 30 per unit. And if you
want fewer than 10 of them, they will cost you 50 each.)
In high-level programming languages much more complicated kinds of
conversions are involved than in an assembler. High-level languages are also
converted into machine language by a program called a compiler. Such compilers
translate the program text (usually called 'source code') into an executable
program - so into machine language. In DOS and Windows such executable
programs usually have the extension .exe.
Of course, the machine language program (the executable) depends on the
kind of system that we want to run it on: the program contains instructions
to one specific processor and calls to one specific operating system.
Such programs usually run on just one system; a program that runs well on Windows is
useless on other systems, such as Unix or MacOS.
It wouldn't make much sense either to have these kinds of programs executed
on the Internet. After all, the Internet is populated by all kinds of
computer systems. If you would like to use
such a (conventional) program to enliven your homepage, you would have to upload versions for Linux, Windows
and various other systems.
There is, however, another problem: Internet users can't risk downloading unknown programs and then
allow them to run on their system. Who, for instance, will guarantee that a program downloaded from the Internet
will not start formatting the hard-disk.
To both problems Java offers a solution:
- Java-programs are platform-independent - which means they can run on
various systems, as long as there is a Java Virtual Machine for
the system.
- With Java you can write special programs for the Internet
(so-called applets), which are so safe that an Internet user doesn't have
to worry about running them.
How does Java work?
With many programming languages, the source-code (the program text in Pascal, C++
or other languages) first has to be converted into machine language before
we can do anything with it.
A Java program has to be converted as well, but not to machine language.
Java programs (i.e. the source-code, with the extension .java)
also have to be translated by a compiler. Java-compilers, however, do not
produce machine language, but something called byte-code (with the extension
.class).
Files consisting of Java byte-code can be run by any system that is equipped
with a Java Virtual Machine.
The Java-compiler is called javac.exe and is part of the Java Development
Kit (JDK).
If we wish to convert the source-code Hello.java into the byte-code Hello.class,
we'll have to issue the following command:
javac Hello.java
The javac-compiler creates the file Hello.class (the applet). This
file cannot be executed by the operating system. It can only be executed by
the Java Virtual Machine (JVM). This JVM is a software environment
comparable to an interpreter (like some older dialects of Basic).
There are two sorts of Java programs: applications and applets.
Applications
The source-code of an application has the extension .java,
and is compiled to byte-code by javac.exe.
An application is not meant to be run on the Internet, but is - as its name indicates - comparable to any kind of program.
Spreadsheets,
internet browsers, games and any other kind of program can be made in the
form of a Java-application.
The principal difference with a conventional program, however, is that the Java application
(just like an applet) is no machine language, but consists of byte-code, which is executed
by the Java Virtual Machine.
Java-applications are preferable to conventional programs in situations in
which several systems have to be able to run the same program.
Applets
Applets are the kind of programs we are going to have a look at in this
tutorial. All good Internet browsers can run applets. An applet
can only be run when it is called by an html-file. Such an html-file can
look very simple, for example like this:
<applet code = Hello.class
width = 320 height = 240>
</applet>
At the Internet site of Sun Microsystems (the makers of Java) you can
download the Java Development Kit (JDK) for free. This development kit
includes a program called appletviewer.exe, which allows you (via an html-file)
to run an applet, as follows:
appletviewer Hello.html
If you have no editor ...
If you have no editor, you can consider using NEWT.
This is a small program with which you can edit and compile lightning-fast.
But don't forget: NEWT does not produce buttons, textfields or code for any
other ready-made objects: you have to
write (or copy) all the code by hand. NEWT is only for those programmers who
know what they're doing.
Read more about NEWT.
Newt can be downloaded here.
To chapter 1
Main Menu Java Tutorial
To home page
(c) 2005, Thomas J.H.Luif
|
|