INTRODUCTION TO PROGRAMMING IN JAVA: THE JAVA PROGRAMMING LANGUAGE

NOTE: This set of www pages is not the set of www pages for the curent version of COMP101. The pages are from a previous version that, at the request of students, I have kept on line.


CONTENTS

1. Object oriented programming languages
2. Origins of Java
2.1. "Java enabled"
2.2. Programming in Java
3. The mechanics of creating an executable Java "application" program
 
3.1. Source code
3.2. Compilation
3.3. Summary
4. Program execution
5. Application Programming Interface (API)
6. Java Environments



1. OBJECT ORIENTED PROGRAMMING LANGUAGES

Java is an object oriented language, i.e. it follows the object-oriented paradigm. Object oriented programming is currently extremely popular although the concept has been around since the early 1970s. A brief overview of well known object oriented languages other than Java is given below (listed in chronological order):

SIMULA. The first language to introduce the concept of objects. Essentially ALGOL'60 with objects. (Not usually considered to be a "real" OO language but significant within the context given here).
 
SMALLTALK'80. The archetypal object-oriented language/environment. Everything in Smalltalk is an object.
C++. Essentially C with object-oriented features. Best be described as a hybrid between a conventional imperative language and an object-oriented programming language.
Eiffel. Designed in 1980s as a simpler and more efficient implementation of the object-oriented paradigm than that espoused by Smalltalk.
C#. New OO lnaguage based on C++ and Java.

There are many more, but the above are the most significant.




2. ORIGINS OF JAVA

The Java language started life in 1990 when a team at Sun, headed by James Gosling, designed a new programming language, known as Oak, for the development of consumer electronics software (the Green Project). It was not till the introduction of the WWW that the language Java came into being. With the introduction of the WWW the Oak development team quickly realised that Oak style programs could be invoked over the Internet using what has become known as a Java Applet. The significance of this is that the language would be completely "platform" independent ---- remember that previously to run (say) a C program on a PC platform you needed a PC compiler and to run the same program on a UNIX platform you needed a UNIX compiler, Etc. To demonstrate this Sun developed the worlds first Java enabled Web browser and called it HotJava.

This platform independence sets Java apart from any other language. However, Java programs do not have to be run from a Java enabled WWW browser, they can be run independently as applications in the traditional manner.

As a relatively young language (compared to languages such as Ada or C) Java is still evolving. The core of the language is small (with respect to other "OO" languages such as C++), however more and more packages (libraries) are constantly being added.

Version 1.0 of the language was launched, by Sun, in 1995. Version 1.1 was produced in 1997, and included minor modifications to the core language and major additions to the libraries. Version 1.2 came out in December 1998 and included many significant changes that resulted in this version being referred to as "Java 2" and then as the "Java 2 Platform". Version 1.2 was followed, in 2000, by version 1.3. The current version (summer 2003) is 1.4, and this is the version we will be using.

 

One way of running Java is to use the Java 2 SDK --- Software Development Kit (previously known, with respect to Java Version 1.1, as the JDK --- Java Development Kit). There are three editions of Version 1.4 of the Java 2 Platform and the Java 2 SDK:

  1. J2SE (Java 2 Standard Edition): The edition we will be using.
  2. J2EE (Java 2 Enterprise Edition): An extended version of the J2SE which has additional facilities to enable Java to be used with distributed systems.
  3. J2ME (Java 2 Micro Edition): A slimmed down version of the J2SE which enables Java to be used in embedded systems such as smart cards, pagers, mobile phones, etc.

2.1. Java "Enabled"

The term Java enabled means that inside the browser there is a hypothetical machine known as a Java Virtual Machine (JVM) which can run Java for that particular computer. The Java that comes over the net when an Applet is invoked is encoded in something known as Java Byte Code (JBC). A JVM resident on a particular computer can interpret JBC and consequently "run" the Applet on that computer.


2.2. Programming in Java

From Section 2 we have seen that we can write two different kinds of Java program:

  1. Applications: Stand alone programs similar in nature to the kinds of programs that might be written in any other high level language (C, C++, Ada, etc.)
  2. Applets (small applications): Programs that are executed from a Web browser such as Netscape Communicator. Applets typically allow the user to control execution through a Graphical User Interface (GUI) --- a collection of components such as buttons and scroll bars within a window. An Applet can be executed from any "Java enabled" browser.

We will be focusing on the first of these approaches.




3. THE MECHANICS OF CREATING AN EXECUTABLE JAVA "APPLICATION" PROGRAM


3.1. Source Code

Using a high level language, such as Java, a program is written as ordinary text stored in a file. This text is referred to as the source code. A file containing text is usually referred to as a text file. Text files are created using a program called a text editor. This is a program the same as any other, input is usually from the keyboard and the output is a text file. Text editors allow users to create and edit text files. There are many examples of such programs (PFE, KWRITE), the more sophisticated are referred to as word processors and are designed to produce documents rather than programs. Text editors can be viewed as simple word processing programs.

It is good practice to give meaningful suffixes to source code files, in some cases (and this includes Java) this is a requirement of the "compiler" (see below). Thus Java source code text files must have the suffix .java; similarly C source code would have the suffix .c and Ada source code the suffix .ada (although this is not a requirement of either C or Ada compilers). A plain text file, containing (say) some notes, might then have the suffix .txt. In this manner you can identify the different files contained in a directory at a glance.


3.2. Compilation

Once a program exists as a text file the next stage is to compile (translate) this text into an executable form. Generally most compilers (e.g. C, C++ and Ada compilers) translate source code into machine code. As noted previously the disadvantage of this is that different compilers will be required for different languages and machines (machine code is machine specific), i.e. languages such as C and Ada are not platform independent. Java, on the other hand, is platform independent because it compiles the source code into Java Byte Code (JBC).

 

As noted above JBC comprises a set of instructions written for a hypothetical machine known as the Java Virtual Machine (JVM). In this manner the Java language achieves its platform independence. We say that Java code is portable in that a Java program will run (without modification) on many types of machine. Portability is an important issue in software development and an often quoted advantage of the Java high level languages. Thus, given a Java source code file called (say) Mypro.java the Java compiler is invoked as follows:

javac Mypro.java

This will produce a Java byte code file called Mypro.class.


3.3. Summary

To summarise the above, the stages in the production of a working Java program are as follows:

  1. Create the program text (source code) using a text editor.
  2. Compile the source code to create an executable file (the load module).

This process is illustrated in Figure 1. Note that in the figure the Java compiler also has input from packages, these are files containing preprogrammed Java classes which come with the Java language and which facilitate common operations such as input and output. Because these operations are so common there seems little point in writing them over and over again for every Java program that we write, and therefore they are provided as part of the language.

(The "packages concept" is common to many programming languages.)

DATA FLOW DURING COMPILATION

Figure 1: The mechanics of creating an executable Java program.




4. PROGRAM EXECUTION

In the case of languages such as C, C++ and Ada, once code has been compiled the result may be run (executed) through a command to the operating system comprising the name of the program (without any suffixes). However, in the case of Java --- to achieve the desired platform independence --- the Java byte code must be interpreted using an interpreter. Assuming the existence of a Java Byte Code (JBC) file called Mypro.class this can be achieved as follows:

 
java Mypro

Java can thus be viewed as a cross between a compiled high level language and an interpreted one.

Note: Remeber that the alternative way of invoking a Java program is by writing it in the form of an Applet and invoking it from a (Java enabled) WWW page --- however this is not of interest to us at present.




5. APPLICATION PROGRAMMING INTERFACE (API)

In Sub-section 3.3 we noted the existence and usage of the "packages" concept. Many programming languages provide a set of packages to support common operations such as Input and Output (I/O). Collectively the preprogrammed classes contained in the Java packages which are provided with the language are called the Application Programming Interface or API (a term also common to many programming language and not just Java). The Java API consists of a large collection of classes (hundreds) organised into different package (each package thus contains a number of classes). Currently (April 2005) the Java API comprises some 30 packages (some of which comprise several sub-packages).

A package can therefore best be described as a collection of classes which logically fit together. The most commonly used packages are:

lang --- for routine operations (this package is always automatically compiled into every Java program).
util --- for additional useful utilities.
io --- for input and output (I/O).
text --- for specialized formatting.
awt --- for graphics and graphical user interfacing.

 
net --- for networking.
applet --- for creating applets. i.e.java programs that tun on the WWW.

We will only be considering the first three of these.

We can inspect individual packages and their constants through Sun's Java WWW page at http://java.sun.com. If you go to this page and select "API Specifications" (listed near the top on the menu on the left under "reference") this will take you to another page ( http://java.sun.com/reference/api/index.html). If, under "Standard Edition", you now select "J2SE 1.5.0" this takes you to an extremely useful page ( http://java.sun.com/j2se/1.5.0/ docs/api/) shown in Figure 2. It is a good idea to add this URL to your bookmarks/ favourites. All the packages currently available are listed here (and all the classes). If you select a package this will bring up a new page listing all the classes in that package. By selecting an individual class you can then inspect the features of that class, and so on. For example if we select the package java.lang (which provides classes that are fundamental to the Java programming language) and then "scroll" down the class summary and select the class system you will see a summary of all the fields and methods contained in this class.

JAVA WWW PAGE

Figure 2: The Java "J2SE 1.5.0 API specification" home page

(Remeber that the java.lang package is always automatically incorporated into every Java program.)




6. JAVA INTEGRATED DEVELOPMENT ENVIRONMENTS

There are a great many Java integrated development environments (IDEs) available (Sun's J2SE is not an environment). Examples include:

  1. BlueJ: Developed as part of a university research project about teaching object-orientation to beginners and therefore probably well suited to COMP101.
  2. Studio Standard 5: Sun's own Java IDE.
  3. Microsoft's Visual J++.
  4. JBuilder: Produced by Borland.
  5. CodeWarrior: Produced by Metrowerks
  6. JCreator.
 

All these are available for PC platforms using Windows 2000 or NT or later. There is also a version of Code Warrior that will run on Macintoshes.

An additional advantage offered by some of these environments is that they can outperform the J2SE in speed. A word of warning however, most environments come with their own built-in Java compiler which may not be the latest version (Java is upgraded by Sun at intervals) --- to upgrade such environments a new version must be obtained --- at cost!.




Created and maintained by Frans Coenen. Last updated 10 February 2015