A Brief Introduction to Java
Author: Andrew Bruce
Last Revised: July 2000
Introduction
Whether you have been programming for years in another programming language or using Java as your first language, this tutorial aims to give a high level glance at Java, how it works, and some basic examples.
For the past few years there has been a lot of hype about Java and the benefits it provides for developing everything from embedded systems to large-scale Object-Oriented systems. Java's features include:
- Large Library set
- Elegant OO programming language
- Portable execution environment supporting the following:
- Portable GUI
- Multi-threading
- Garbage Collection
- Security
- Internet capability
- Database connectivity
- Distribution
Although there are a lot of benefits of learning Java, there also seems to be a lot of confusion. Java is not the silver bullet of programming languages. Nor are you going to learn Java over night, especially if it is your first programming language. Java does however make the concepts of OO programming quiet easy to understand, and allows close modelling to real life objects.
History of Java
Java, originally named Oak (1990), was aimed at embedded systems, such as Toasters, and other household items. With this heritage Java may not sound like a serious programming language, but Java's ease of programming, automatic garbage collection and memory allocation, set it apart from other programming languages and make it ideal for Internet applications. When Java was first developed the designers required a language that was:
- Machine independent
- Reusable
- Overcame the common problem of Memory leaks (common with C++)
Sun Microsystems while working on a project called green, had identified the need for a new programming language and thus Java was developed.
How Java Works
The normal paradigm with program development is that program source files are written and then these source files are passed through a parser and compiled into a Operating System (OS eg. Window 98, UNIX, Linux) specific executable files. So different platforms, require different executable files to run. Java on the other hand does not have this problem; one single version of the code is compiled into Byte code and distributed. Each operating system requires its own Java byte code interpreter, which runs all Java files.
To put into different words, Java programs are complied into byte code files known as class files (and having a .class file extension). Class files are cross platform or non operating system specific. You require a Java interpreter that is OS dependent to execute the byte code. The advantage is that once you have downloaded the Java Interpreter, you can execute any Java class file, independent of being concerned with which computer type it was originally written on or designed to run on.

Java and the Web
It was Java's cross platform ability and ease of integration into the Internet's current
infrastructure that was one of the catalysts for Java's sudden growth.
Java allowed web pages to have dynamic interaction with the user.
Please note here we are not talking about JavaScript, rather Java applets.
Java applets can be thought of secure Java programs that load across the
Internet and run in a browser. As with Java applications, there is one single version
of the source that runs on all machines, as the diagram below illustrates.

Java's ability to also handle Unicode allows Java to support multiple (human) languages
including double byte characters (Asian languages are double byte). One of the biggest problems with HTML
on the internet is its lack of support for dynamic interactivity with the user.
HTML by nature is totally static and each time a user goes to the site the
content is sent without any interaction. Java applets are client side applications,
which run on the client's machine (as opposed to Java Servlets which are server side).
Most new browsers support Java 1.1 and support JDK 1.2 via a plug-in.
It is the browser that interprets Java applets (unless you run appletviewer from the command prompt)
and displays them embedded within an HTML document. Below is a brief example of a Java Applet
which simply displays the message "I was written in JAVA". The first code listing is the HTML source followed by the Java source.
HTML Source
<html>
<head>
<title>Java Applet Example</title>
</head>
<body>
<applet code="MyFirstApplet.class" width=300 height=300></applet>
</body>
</html>
Java Applet Source
import java.applet.Applet;
import java.awt.Graphics;
public class MyFirstApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("I was Written in Java", 25, 25);
}
}
The function drawString takes three arguments. The first argument is the "Text to Display",
the second is the "Horizontal Distance from Top left as an integer", and the third is
"Vertical distance from Top left as an integer".
Your First Application
Java applets are designed to allow Java programs to be loaded across the Internet and run locally within a secure environment within a browser.
Java also supports running programs outside a browser. Such Java programs are called applications.
Java applications, although similar in syntax to applets do have some differences. Java applets have a special life-cycle
method (called init()) designed to facilitate them being called from a browser.
An application's lifecycle is independent of any browser (they require a main()
method which is called as the application is started).
Below is a Java application and a Java applet that both do the same thing (see the side bar for details on how to compile and run the application).
Before executing a java application you must download a java byte code interpreter.
http://www.sun.com/java
Once you have the JDK (Java Development Kit) installed on your PC, you can use any third party text editor or by default Note Pad to input your Java source code.
Once you have written your Java Application, it will require compilation and execution. Listed are the normal steps involved
- From a command prompt change into the directory where you saved your new Java program.
- Type at the prompt:
javac filename.java (replace the filename)
- Presuming that there are no errors during compilation you should now be able to execute your java program by typing:
java filename
A Simple Java application looks like this:
import java.awt.*;
class MyFirstApp
{
public static void main(String[] args) {
System.out.println("My First Java Application");
}
}
Dissecting the code above the first line:
import java.awt.* means import all the classes from the package java.awt
class MyFirstApp sets up the class name for my class
Thinking in Objects
One of the main programming paradigms used to construct Java programs is Object-Oriented
design and construction. To get you on your way to programming in Java, it is necessary
to understand how Java objects and classes operate.
As an example, consider a Car Class. A car has attributes such as engine capacity,
colour, wheel type, number of doors. The Car class can be thought of as a type of abstract
car (attributes that all cars have in common). Now when you decide that you wish to construct
a car object, you take into consideration the attributes above to make an instance of a car, a Ford
Capri for example. We would make an instance of car and set its engine capacity, colour,
number of doors, etc. First, let's create the car:
Car myCar = new Car( );
Now that we have created a car called myCar, we have to assign particular attributes to our new car object Our car object's paint attribute needs to be set so our new car can have its colour scheme
We would set it by:
myCar.paint(red);
Now you might be thinking this sounds too simple to be actual java code, well believe it or not, it really is java code.
Let's consider a larger example which allows the user to set the background colour of an applet via a set of buttons.
import java.awt.*;
public class SetBack extends java.applet.Applet
{
Button redButton, blueButton, greenButton, whiteButton, blackButton;
public void init() {
setBackground(Color.white);
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
redButton = new Button("Red");
add(redButton);
blueButton = new Button("Blue");
add(blueButton);
greenButton = new Button("Green");
add(greenButton);
whiteButton = new Button("White");
add(whiteButton);
blackButton = new Button("Black");
add(blackButton);
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button) {
changeColor((Button) evt.target);
return true;
} else {
return false;
}
}
void changeColor(Button b) {
if (b == redButton) {
setBackground(Color.red);
} else if (b == blueButton) {
setBackground(Color.blue);
} else if (b == greenButton) {
setBackground(Color.green);
} else if (b == whiteButton) {
setBackground(Color.white);
} else {
setBackground(Color.black);
}
repaint();
}
}
Examining one of the lines:
if (b == whiteButton) {
setBackground(Color.white);
}
We can interprete this line of code in plain english as follows:
If the object b (which is a button representing the button that was pressed) equals the instance of whiteButton (the one labelled 'white') then set the background colour to white
Now let's see this applet in action:
As you can see, Java does not require a vast amount of programming experience
to get started nor does it require you to think like a software Engineer.
Object-Oriented Programming Languages such as Java, allow people to think in terms of real objects
(a Falcon is of type car and inherits all the attributes of car).
With Java being platform independent more and more software systems are going to be built using Java technology.
It is hoped that this tutorial, although brief, will help give enough overview of the
language to encourage you to persue further training and join the ranks of Java Programmers everywhere.
Listed below are links to example java code (Please note that you will need to use Winzip, or another unzipping program, to unpack the files):
Java Background Changer
MyFirstApp Example
MyFirstApplet Example
Related Information
If you have successfully completed this tutorial why not consider taking ASERT's Java training courses. For more info, see: Training Course Info.
If you need help with Java Mentoring or Software Development why not consider ASERT's consulting and mentoring services. For more info, see: Professional Services Info.
|