topical media & game development

talk show tell print

object-oriented programming

The language Java

Java has a direct heritage from C++. Started as an interpreted C++ like language under the name Oak, it became a hype due to its introduction as the dial-tone of the Internet in 1995.

Java -- the dial-tone of the Internet


   1995   Introduction at WWW3
   1996   1.0 with AWT
   1997   1.1.x with modified event handling
   1998   version 1.2 (beta) with Swing

Design principles -- safety

  • a modern programming language
  • C++ syntax, no pointers
  • virtual machine (runs on many platforms)
  • libraries: threads, networking, AWT
  • downloadable classes
  • support for applets
  • extensions and APIs: Beans, Swing, MEDIA, 3D


slide: The language Java

The impact of Java has been enormous, not only in terms of its adoption in the software industry, but also in terms of the number of books written and sold. Java has also become the language of choice for introductory programming courses.

The original design purpose stated for Java was to provide a safe environment for executing so-called applets, written in a general purpose programming language, in a Web-browser. For that reason Java programs are executed by a virtual machine, in which the programs can be executed as in a sandbox protecting the environment from possibly malicious programs. In addition, the virtual machine gives a high degree of platform independence. The slogan that has accompanied the introduction of Java, write once, run everywhere does nevertheless not completely hold true when it concerns user interfaces, since the particularities of the various platforms have proved to be hard to master.

Whatever one may think of Java as a language, a lot of effort is put into coming to an agreement about the numerous APIs that are part of the Java platform. These include the Swing GUI framework, the component Beans, the reflection API, the native code interface, the multimedia framework, not forgetting the Java3D and VRML classes. This makes the Java platform indeed a very powerful and productive environment.

Java is very well documented. Apart from the many books about Java, there is also an excellent tutorial online, for free. See the URLs in slide appendix-java.


In comparison with C++, Java is almost as rich in keywords. Notably lacking, however, is the keyword virtual. This is not needed, since in Java every method is by definition subject to dynamic dispatching. Also, since Java doesn't allow multiple inheritance, there is no need to use it for avoiding multiple copies of inherited base classes.


Java overview

  • new, finalize, extends, implements, synchronized

Language features:

  • no pointers -- references only simplify life
  • garbage collection -- no programmers' intervention
  • constructors -- to create and initialize
  • single inheritance -- for refinement
  • interfaces -- abstract classes
  • synchronized -- to prevent concurrent invocation
  • private, protected, public -- for access protection

slide: Java -- terminology (1)

New is the keyword interface in Java. Defining an interface is equivalent to defining an abstract class in C++. One merely lists the methods provided by the (abstract) claas, without providing an implementation. A concrete class may then indicate that it implements the interface. In this way multiple (interface) inheritance is supported in a nice and clean way.

Also new is the keyword synchronized, reflecting the built-in support for concurrency in Java. A synchronized method excludes multiple invocations of that method, which might otherwise occur in a multi-threaded program.

The keyword final may be used to indicate that a particular value may not be changed. In this sense it is similar to the C++ keyword const. It must be noted that Java is even more elaborate in the use of the keywords private, protected and public than C++. They are used to indicate access restrictions for the methods of an object for objects inside and outside the package in which the objects' class is defined. The language features offered by Java resemble those of C++. However, in many respects Java is much simpler than C++. Most notably, the absence of pointers, a sure source of errors, makes programmers' lives easier. In particular since Java offers automatic garbage collection, programmers need not to worry about disposing objects created dynamically. Resource management, however, may be done by defining a method finalize. The counterpart, however, of that is that all objects in Java come into existence by explicit dynamic creation.

The availability of interfaces compensates for the restriction to single inheritance, which must be indicated by the keyword extends instead of the colon. It has often been argued that multiple inheritance is not really necessary. This holds true, however, only for implementation inheritance. Multiple (interface) inheritance is a powerful feature that has interesting applications once one has discovered how to use it.


Java supports basic types (including int, char and float) and compound types similar to C++. It does not offer pointer or reference types. However, it offers Array and String types. No need to say that it also allows for user-definable classes.

Type expressions

  • basic types -- int, char, float, ...
  • Array -- int ar[SIZE]
  • String -- String[] args
  • class -- user-defined

slide: Java -- expressions (1)

It will be no surprise that the expressions also are similar to those of C++. However, Java does not allow for operator overloading. That means that the operators, as listed below, may only be used for the built-in types.

The operators defined for the built-in types do sometimes behave in an unexpected way. For example, whereas the + operator defined for String concatenates two strings (as expected), the comparison operator == does not compare the values of the two strings, but instead the (opaque) references. One must use s1.equals(s2) to compare the values of the strings s1 and s2.


  • operators -- + , - ,.., < , <= ,.., == , ! = ,.., && , ||
  • indexing -- o[ e ]
  • access -- o.m(...)
  • in/decrement -- o++, o--
  • conditional -- b?e1:e2


  • var = expression
  • modifying -- +=. -=, ...

slide: Java -- expressions (2)

Value expressions may be created using arithmetic and comparison operators (including == for equality and != for inequality).

As logical operators Java includes, as C++, conjunction (&&) and disjunction ( || ), as well as a number of bitwise logical operators. Also, we have an indexing operator which, unlike for C++, may be not defined for arbitrary types. Access to both static and dynamic methods involves the use of the dot operator.

The increment and decrement are defined only for the scalar types. Also, we have a conditional expression of the form b?e_1:e_2 testing the condition b to deliver e_1 when it evaluates to true and e_2 otherwise.

Assignments in Java, like in C++, are written as var = expression with a single = symbol. As remarked previously, this convention is known to cause mistakes by programmers raised with languages such as Pascal or Modula-2.

In addition, Java offers, like C++, modifying assignments, which may be used as, for example, in n += 1, which is identical in meaning to n = n + 1.


Java provides roughly the same elementary control structures as C++.


  • conditional -- if (b) S1; else S2;
  • selection -- switch(n) { case n1: S1; break; ... default: ... }
  • iteration -- while (b) S
  • looping -- for( int i = 1; i <= MAX; i++) S
  • jumps -- return, break, continue

slide: Java -- control

These include a conditional statement (of which the else part may be omitted), a selection statement (that allows for a default branch), an iteration statement (which is also offered in a reversed form to allow a repeat), a loop statement (consisting of an initialization part, a part to test for termination, and a repetition part to increase the loop variable) and, finally, jumps.


An object is an instance of a class. In Java, a program is executed by calling the static main method a class. Look for example at the HelloWorld class in slide Java-objects-1.

Hello World -- Java (1)

  public class HelloWorld {
  public static void main(String[] args) {
         System.out.println("Hello World");

slide: Java -- objects (1)

The HelloWorld example is taken from a collection of hello world programs located at . See hello for a sample of this collection.

To illustrate the use of interface definitions, slide Java-objects-2 presents a slightly modified version. The actual HelloWorld class announces that it implements the World interface.

Hello World - interface

  public interface World {
  public void hello();

Hello World - class

  public class HelloWorld implements World {
  public void hello() {
         System.out.println("Hello World");

slide: Java -- objects (2)

Both classes make use of the standard out stream defined in the class System to emit their message to the world.


In Java, all classes are derived from the class Object, defined in the package java.lang. As an advantage, every object class defined in Java is known to support a number of basic methods, such as clone, equals, finalize, hashCode, toString. Of course, the programmer of the class is responsible for redefining these methods if appropriate. For example, when the method clone is invoked, the object throws an exception unless the method clone has been defined.

The following members of Object, however, cannot be overridden: getClass, notify, notifyAll, wait. The latter three reflect Java's support for threads; they should not be used when the object is not a thread.

Hello World -- Java (2)

  import java.awt.Graphics; 
  public class HelloWorld extends java.applet.Applet { 
  public void init() { resize(150,50); } 
  public void paint(Graphics g) { 
         g.drawString("Hello, World!", 50, 25); 

slide: Java -- inheritance

A simple example of a class that inherits from the Java Applet class is given in slide Java-inheritance. It redefines the init and the paint method, which says `Hello World'.


The Java platform, that is the Java language enriched with the numerous libraries, frameworks and extensions, offers the software developer a rich environment for developing (distributed) Internet-aware applications.

Perhaps the most well-known feature of Java is its support for so-called applets, light weight applications that may enrich your Web-browser with graphics, multimedia and additional communication facilities. Browsers such as Netscape and Microsoft Internet Explorer have an embedded Java Virtual Machine that enables them to execute Java applets. Applets may also be executed by the Java plugin that has been provided by Sun Microsystems as an alternative to the browsers' built-in virtual machines.

Java -- techniques

  • applets -- extend your browser
  • servlets -- extend your server
  • networking -- urls, sockets
  • RMI -- remote method invocation
  • reflection -- meta-information
  • beans -- component technology
  • JNI -- writing native methods
  • javadoc -- online class documentation

slide: Java -- techniques

Server-side extensions are made possible by servlets. The server must then be written in Java, or provide for a virtual machine.

Java offers a number of facilities for networking, including support for retrieving resources by URL, sockets, and remote method invocation. Remote method invocation (RMI) may be considered a light weight alternative for CORBA distributed programming.

In contrast with CORBA (version 2.0), Java allows for sending objects over the network due to its powerful Reflection API that gives runtime access to the properties of objects, including class types and methods.

The Beans framework offers component technology, that allows developers to exchange (beans) objects and inspect their properties in a uniform manner. For example GUI elements, written as beans, can be incorporated at runtime to add the desired functionality to a user interface.

Another well-designed and powerful feature of Java is its native interface, which enables the experienced programmer to embed native code in Java applications. No need to say that from a purists' point of view one should avoid this.

Last but not least, the javadoc facility must be mentioned. The javadoc tool allows for creating documentation directly from the class definitions, that may be annotated with signature descriptions, and information about its author, possible exceptions and comments.


This section has presented an overview of Java.

The language Java


  • design principles -- safety
  • terminology -- object, interface
  • syntax -- like C++
  • objects -- abstract data types, interfaces
  • inheritance -- single inheritance
  • techniques -- dynamic casts, reflection, APIs

slide: Java -- summary

It gave an outline of its history, and discussed the design principles underlying Java and its possible future as the dial-tone of the Internet.

It described how Java differs from C++, gave an Hello World example of an ordinary program, an object that implements an interface and an applet. Also, we discussed briefly the libraries, frameworks and extensions comprising the Java platform.

(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.