Introduction to Java

estrapadesherbetΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 4 μήνες)

62 εμφανίσεις

Introduction to Java

Java can be used to create standalone
programs in the same way as C++ or to
create applets which run within a Web

This course will focus on creating applets

Before a user can run a Java applet, they
must be running a brow
ser that supports
Java (a “Java
enabled” browser). Examples
are Netscape Navigator and Microsoft
Internet Explorer

“Java is an object
oriented, robust,
secure, multi
threaded, interpreted,
platform independent language”


Same concept
s as in C++

Main features are encapsulation,
polymorphism and inheritance


A class could be described as a collection of
data and methods

The methods implement the operations
which can be performed upon the data

The data can only be acces
sed by methods in
the same class

Three main reasons for doing this:

Data hiding

Data security

Faster upgrading


“having many forms”

Usually refers to identically
named methods
that have different behaviour depending
upon the type of ob
ject that they reference
i.e. “method overloading”

Covered in more detail later on in the course


Allows you to re
use existing classes by
extending or customising them

usability is a fundamental OO concept. It
saves programming time so

you can create
new applications/applets much more quickly

Java has existing super classes e.g. “Applet”

“Robust ”

It’s still possible to write bug
software with Java but certain types of
programming errors are eliminated

More robust than C++

More strongly

extensive compile
time checking for type mis

All accesses to arrays and strings are
checked at run
time to ensure that they are
within bounds, eliminating overwriting
memory and corrupting data

No pointers so pointer r
elated bugs are

Casts of one type to another are checked at
runtime to ensure that they are legal

Automatic garbage collection

Simpler error handling and recovery with the
exception handling feature

“Secure ”

Downloading programs to a us
ers web site
creates security problems:

The program could introduce a virus onto
the user’s computer

The program could transfer information
stored on the user’s system back to the

Java eliminates these problems by not
allowing a Java applet to re
ad from or write
to files on a user’s system

threaded ”

The instructions that are being executed by a
program can be called the program’s “thread
of execution”

You may want a program to perform more
than one task at the same time. For example,
n applet could spin a logo as it flashes the
name of a company on the screen while yet
another image is being downloaded by the
browser. One thread could spin the logo,
another thread could flash the company’s
name and yet another thread could download
e image

Of course, the processor can really only
execute one thread at a time but by “time
slicing” will give the impression that all
three threads are being executed at the same

It is possible to use multi
threading with
C++ but Java makes it much

“Interpreted and platform

A Java program will run on any system type
e.g. PC, Mac, UNIX based i.e. it is platform
independent so the applet programmer only
needs to create one applet

Other language compilers create machine
code wh
ich is processor specific. Java is
different. The Java compiler produces an
intermediate code called “virtual machine
code” (VMC)

VMC is not processor specific so there is
another stage to go through before the
program can be run by a particular
. This is done by the browser when
it downloads the VMC. The browser
interprets the VMC into specific machine
code for the processor which is being used
by the user.

Because of this two
stage conversion
process, Java programs will not run as fast
as those

compiled for a specific processor

A First Java Applet

// a first program in Java

import java.applet.Applet; //import Applet class

import java.awt.Graphics; //import Graphics class

public class HelloWorld extends Applet


public void paint(Graphics g


g.drawString(“Hello World”,5,25);




indicates a programmers comment (as in

import statements

Import the necessary classes into your program.
These lines tell the compiler that class Applet is
to be found in package java.applet a
nd that class
Graphics is found in package java.awt

Importing the Applet class allows you to build
your own applet

Importing the Graphics class allows your applet
to draw graphics on the screen


public class HelloWorld extends Applet

This is a class


keyword introduces the definition


is the name of the class. When you
save an applet in a file the file name must be the
same as the class name with a .java extension so
the example file name would be It
is case sensitive.

extends Applet

indicates that the new class will
be derived from a super (base) class named
The Applet class provides the basic
functionality that every Java applet needs.

keyword allows the browser to create
an ins
tance (an object) of the class HelloWorld.
Java also has


and these are used much in the same way as
C++. These are covered in more detail in later


{ }

Begins and ends the body of a class or method

public void paint(Graphics g)

Introduces a method into the HelloWorld applet


allows the browser to use it

indicates that this method will not return
any results

is the name of the method. The paint
method’s task is to draw graphics

on the screen.
This method is different to ‘normal’ methods as
it does not have to be called by the applet. It is
spontaneously called by the browser when it
starts up the applet and again if the applet
becomes covered by another window and then
ed. The paint method can be called by
an applet but it is not recommended. If different
information is to be displayed on the screen, for
example after an input from the user, then the

method should be used instead. This
method erases any informa
tion that was
previously displayed on the screen and then calls


(Graphics g)

The paint method needs a
Graphics object to be able to execute it’s task and
this is passed as a parameter named
Again, the
applet does not have to explicitly pa
ss this
information to the method because the browser
will do this automatically when it uses the

g.drawString(“Hello World”,5,25);

This is the only programming statement in the

The Graphics object

calls its

method. The
call has three parameters: the
character string to be displayed and the two
screen co

As you can see, unlike paint ,drawString is not
called automatically

Another Java applet

import java.applet.Applet; //import Applet class

import java.awt.G
raphics; //import Graphics class

public class HelloWorldWithFont extends Applet


public void paint(Graphics g)


Font font = new Font(“Arial”,16);


g.drawString(“Hello World”,5,25);



As you can probably work out, this time “Hello

World” will be displayed in 16 point Arial font

The paint method creates a

object named
with the specified values. The Graphics
class has another method named

and so
the Graphics object
can call this method.

Clearly, the setFont met
hod needs a Font object
to be able to execute its task and so font is passed
to it when the method is called

An applet which uses variables

import java.applet.Applet; //import Applet class

import java.awt.Graphics; //import Graphics class

public c
lass AddTwoNumbers extends Applet


int number1= 2;

int number2 = 3;

int sum;

sum = number1 + number2;

public void paint(Graphics g)


Font font = new Font(“Arial”,16);


g.drawString(“The sum is ”,+ sum,5,25);



As you can see, u
sing variables in Java is very
much like using variables in C++

You can use a combination of letters, numbers
and the under
score for a variable name. You
cannot begin a variable name with a number.
Java is case

Notice the use of the + sign b
efore the name of
the variable
. This is necessary because the
method drawString does what it says

it draws a
string of characters. The value of sum is
implicitly converted to a string and this is then
concatenated to the string “The sum is ”.

ing an HTML file

A Java applet runs within a browser so you will
need to create an HTML file to load the applet
into the browser

The following HTML file will load the first
example program into the browser:


<TITLE> HelloWorld Applet </TITLE>

LET CODE = “HelloWorld.class” WIDTH =


HEIGHT = 55>



Lines 1 and 5 indicate the beginning and end of
the html file

Line 2 indicates the title of the html file

Lines 3 and 4 are special tags for Java applets.
They tell the browse
r to load a specific applet
and define the size of the applets display area in
the browser. generally, an applet should be less
than 640 pixels wide and 480 pixels tall

The first piece of the applet tag
indicates that the file
loWorld.class contains the compiled applet
class. When you compile a Java program each
class is put into a separate file that has the same
name as the class with a .class extension

The html file does not have to be given the same
name as the applet but it

makes sense to give
these files meaningful names.

The JDK provides a special program called the
with which you can test your
applets. This is like a browser that only knows
how to interpret Java applets

The appletviewer is invoked for the H
class from the computers command line as
follows:(assuming the html file had been saved
as HelloWorld.html)

appletviewer HelloWorld.html

Further instructions on html files will be given, if
you need them, in labs