Programming Distributed Collaboration Interaction Through the World Wide Web

University of Calgary, Department of Computer Science
MSc Thesis © Roberto A. Flores June, 1997




CHAPTER 3
The Java Programming Language



In 1985, researchers at Sun Microsystems were working on an innovative windowing system called NeWS, which stands for Networked Extensible Window System. This system was implemented as a distributed system with client computers running lightweight processes that communicate with server applications using messages (Gosling, Rosenthal and Arden, 1989). Typically, client processes perform as receivers of user events that are translated into commands and transmitted to server processes. In response, server processes return programs for the client to execute. These programs can perform operations on the display and receive events from the keyboard and the mouse, thus, repeating the interaction process. Programs downloaded from servers are created to use the PostScript programming language (Adobe, 1985), which is an interpreted language capable to provide portability of code between different computers and operating systems.

NeWS never caught enough market share to succeed and the project was canceled at the beginning of the 1990's, resulting on the merging of team members into other projects. One of these projects, which had the mandate to develop software for consumer electronics, was the originator of the Java language. When C++ proved not to be suitable for the task assigned to that project, the Java programming language was created. Even if NeWS is not strictly a predecessor of Java, the experience gained from the development of NeWS may have helped to shape the features of the Java programming language.


3.1 OVERVIEW.

A programming language is usually characterized by its main features. Java is depicted as an object-oriented, distributed, secure, multi-threaded and portable programming language. These characteristics are detailed on the following sections.


3.1.1 OBJECT-ORIENTED.

Java is an object-oriented programming language. For programmers, this means that they will need to focus on the application data and on the methods needed to manipulate that data, rather than concentrating on functions and procedures. Java was designed with features found on previously developed object oriented languages. However, it manages to strike a balance between pure object oriented models, such as SmallTalk (Goldberg and Robson, 1989), and non-object oriented models, such as C.

Booch (1994) described several features required for a programming language to be labeled as object oriented:


3.1.2 DISTRIBUTED.

Java is a distributed programming language. It supports both the TCP/IP (Transmission Control Protocol/Internet Protocol) and the UDP (User Datagram Protocol) families. From these protocols, TCP/IP is used for reliable stream-based communications, and UDP to support fast point-to-point datagram-oriented models. Java networking classes also include classes to handle Internet addresses and to download the contents of resources associated with a URL.


3.1.3 PORTABLE.

Java has the characteristic of being portable, or more accurately said, programs produced on Java can be executed on any computer where a Java Virtual Machine is implemented. This capability of being portable is based on Java's platform neutrality and interpreted nature.

Java's cornerstone to allow portability is based on a proprietary set of intermediate instructions called bytecode, which are used to conform all Java programs. Bytecode are sequences of bytes representing instructions for the Java Virtual Machine, which is a simulated CPU implemented on Java interpreters. In practice, when an interpreter loads a program, each byte is evaluated in software, performing changes on the state of the virtual CPU to reflect the changing state of execution on the program.

Additional characteristics that support portability in Java are the abstraction of primitive data types and graphical user interfaces. In the case of primitive data types, as shown in Table 3, they are specified to be of a fixed size regardless of the operating system of execution.

Type
Contains
Size
Minimum Value
Maximum Value
boolean
true or false
1 bit
Not Applicable
Not Applicable
char
Unicode character
16 bits
\u0000
\uFFFF
byte
signed integer
8 bits
-128
127
short
signed integer
16 bits
-32768
32767
int
signed integer
32 bits
-2147483648
2147483647
long
signed integer
64 bits
-9223372036854775808
9223372036854775807
float
IEEE 754 floating-point
32 bits
±3.40282347E+38
±1.40239846E-45
double
IEEE 754 floating-point
64 bits
±1.79769313486231570E+308
±4.94065645841246544E-324

Table 3. Java Primitive Data Types.

To handle user interfaces, Java designers developed an abstract windowing library that acts as a wrapper for native widgets found on major graphical environments. This way, the use of components is unified under a single set of classes, which are independent of the platform of execution. Figure 4 shows the class hierarchy for widgets available on the core release of Java. These components represent just a portion of the entire abstract windowing library.


Figure 4. Component classes from the Abstract Window Toolkit library.

Classes found on Java allow the creation of buttons, canvases, checkboxes, radio buttons, labels, list boxes, combo boxes, scroll bars, input lines, input areas, windows, panels, dialogs, windows, and the practical file dialog to select disk files.


3.1.4 SECURE.

Java is intended to be a secure language. Security is an important concern, since Java is targeted to networking environments. Based on the premise that no downloaded program is to be trusted, Java implements several security mechanisms to protect users against malicious code.

When compiled, Java source code is checked for compliance with the memory allocation and reference model. Under this model, declarations for direct access to memory addresses are not allowed. Additionally, memory layout decisions are not made at compilation time. Instead, compilers will generate handles that will be resolved to real memory addresses at runtime, preventing programmers to hack into systems using such addresses.

Even though the use of Java compilers ensures that source code will behave according to safety rules, interpreters do not have the means to check that any downloaded bytecode was produced by a well-behaved compiler. To trust downloaded code, interpreters will subject programs to verification through a series of tests. These tests range from simple verification of the format on instructions to validating the code through a simple theorem verifier. Once the verification process is done, interpreters can proceed to execution knowing that the code will run securely. For detailed information on the verification process please refer to the Java Security section back to Chapter 2.

Unfortunately, the verification process in Java is not as secure as it is claimed, since it fails to have formal semantics and a formal description of the type system. This circumstance makes it impossible to formally prove the correctness of the runtime verifier (Dean, Felten and Wallach, 1996). As a result, the verification process can not be proven correct since its exact behavior for every possible set of bytecode is uncertain.


3.1.5 MULTI-THREADED.

Java is a multi-threaded language. It provides support for multiple lightweight processes within a program. The main problem with writing multi-threaded programs resides on making methods safe to be accessed by multiple concurrent threads. This task usually implies the management of locks to control and synchronize access to resources.

Java supports pre-emptive multi-threading at the language level and through the support of the runtime system and thread objects. Multi-threading is supported at the language level by using locks -- or monitors -- for synchronization. Every class and variable has a lock that can be used for this purpose. For example, methods within a class that are declared synchronized do not run concurrently. This behavior is automatically enforced by granting the class lock to the first thread entering a synchronized method. The lock will be released by the thread when exiting the method or when put to sleep. Support for threads at the class level is provided by the Thread class, which implements methods to start, stop and handle threads, and the Runnable interface, which provides the abstraction required for an instances of a class to be treated as a thread.


3.2 PROGRAMMING FOR THE INTERNET AND THE WORLD WIDE WEB.

Rather than creating new HTML extensions, Java made popular the notion of downloadable programs that can run inside Web browsers. The alpha release version of Java, back in 1995, included a Web browser called HotJava. This browser allowed normal Web navigation plus the ability to execute Java applets hyperlinked to HTML documents. Shortly thereafter, Netscape announced its intention to license Java to integrate it to its second version of its market-leading Navigator browser.

HotJava and Netscape Navigator are not the only browsers that support Java applets, but they were first in order of appearance and current market share, respectively. Both these browsers have promoted the use of Java as a programming language for the Web. However, HotJava and Netscape Navigator have followed different patterns of development and, up to the day of writing this thesis, HotJava has only reached beta release status while Navigator is at the brink of version 4. Due to its wide availability and advanced state of development, Netscape Navigator will be chosen for further studies on the integration of Java to the Web.

In order to evaluate the suitability of Java as a programming language for the Web, two characteristics have to be observed: first, the level of integration with browsers, and second, the availability of tools to perform distributed operations.


3.2.1 INTEGRATION WITH NETSCAPE NAVIGATOR.

Netscape has developed LiveConnect (Netscape, 1996c) to act as an integrator between JavaScript and Java. LiveConnect allows JavaScript code to access variables, methods and classes found on applets embedded on Web documents. Java classes do not need any special settings to be invoked from JavaScript.

<SCRIPT>
function handleEvent(id, value1, value2, value3)
{
   document.jKSImapplet.handleJavaScriptEvent(id,
                                              value1,
                                              value2,
                                              value3)
}
</SCRIPT>

<FORM NAME=Options>
   <SELECT NAME=listNEW>
      <OPTION SELECTED> Node - Rectangle
      <OPTION> Node - Rounded Rectangle
      <OPTION> Node - Ellipse
      <OPTION> Line - Binary
      <OPTION> Line - Trinary
      <OPTION> Line - Quadrary
      <OPTION> Context Box
   </SELECT>

   <INPUT TYPE="BUTTON" VALUE="New"
      onClick="handleEvent(0,
                           document.Options.listNEW.selectedIndex,
                           0,
                           null)">
</FORM> 
     
Figure 5. Code example for a Java method invocation from JavaScript.

Figure 5 shows a code example where a Java method is invoked from a JavaScript function embedded on an HTML document. In this example, the method handleJavaScriptEvent, which belongs to the jKSImapplet Java class declared on the current document, is invoked after a button (part of an HTML form) is pressed to process choices made on a selection box. As seen from the function handleEvent, the invocation of Java methods just requires the definition of a hierarchical path for locating applet's classes and variables.

However, enabling communication in the other direction (from Java to JavaScript) requires a little more effort. In such case, Java applets will need to subclass objects derived from the JSObject and JSException classes, which are part of the javascript package provided by Netscape. From these classes, the JSObject class enables Java applets to access JavaScript methods and properties, and JSException allows the handling of exceptions thrown by JavaScript code returning an error.

Figure 6 shows a code example taken from the LiveConnect introduction documentation, where the eval method from the class JSObject is used to send an expression to JavaScript each time the mouse button is released. In this example the expression sent is an invocation to the alert JavaScript method.

Public void init()
{
   JSObject win = JSObject.getWindow(this);
}

public boolean mouseUp(Event e, int x, int y)
{
   win.eval("alert(\"Hello world!\");");
   return true;
}
        
Figure 6. Code Example for a JavaScript method invocation from Java.


3.2.2 INTERNET NETWORKING.

The java.net package included as part of Java provides the infrastructure needed to achieve networking operations. The basic protocols to deal with the Internet are implemented in a few classes that encapsulate their functionality without involving the programmer with low-level networking details. Classes included in this package allow one to represent Internet addresses, to access resources referenced by URLs, to perform low-level networking using datagrams, and to communicate using stream sockets.

Basic classes required for networking operations are URL and InetAddress. These classes, and their importance to initialize other classes, are explained as follows:


3.3 LANGUAGE COMPARISON BETWEEN JAVA AND C++.

Java is a language that borrows much of its terminology and syntax from C++. However, Java is considered a simpler language than C++, since a number of C++ features have been removed from the Java implementation. In certain ways, this reduction allows programmers, familiar with C++, to easily climb the learning curve. Java eliminates some C++ redundancies and non-object-oriented characteristics maintained as legacy from C.

A number of main differences exist between Java and C++. These differences, which range from slight modifications to complete removal of features, are described as follows:


3.4 CHAPTER SUMMARY.

This chapter covered the fundamental aspects of the Java language, which was described as an object-oriented, distributed, portable, secure and multi-threaded programming language. After discussing these characteristics, Java was scrutinized to find its suitability as a programming language for the Web.

This chapter also showed Netscape's LiveConnect environment as a fundamental part to integrate Java with the Navigator browser. The use of JavaScript and Netscape-tailored Java classes were discussed as required elements to achieve this integration.

Additionally, Java networking classes were explained. Classes that handle Internet addresses and Uniform Resource Locators were introduced as the basis to support datagram and socket communications, and URL connections.

This chapter concluded with a section detailing the main differences between Java and C++. In its role as the most popular programming language, C++ is compared with Java with respect to aspects ranging from the structure of primitive data types to templates and automatic memory management.

Chapter 4 is an overview of the Java concept mapping tool implemented as a test case for this research. This chapter will show previous concept mapping tool developments, as well as the motivation underlying such developments. The system architecture for the test case, which has been named jKSImapper, is further discussed in Chapter 5. That chapter will also discuss lessons learned when porting previously developed C++ classes to Java.



© Roberto A. Flores June, 1997