JAVA OVERVIEW



This is a very brief overview of Java that will be of most value to those who are familiar with both Smalltalk and C++. As the tutorial is developed further tha more complete introduction to Java will be included.

In general, Java has Smalltalk semantics and C++ syntax. There are no pointers, because there is no static data. All objects must be allocated (with the operator new), but there is no explicit deallocation. Instead, there is a garbage collector that runs continuously as a thread in the background. All classes are descended from the general class Object.

Unlike Smalltalk, Java contains both primitive data types (int, boolean, etc.) similar to those in C++ and classes whose instances are objects. There are also classes that are "wrappers" for the primitive data types, which allows primitive data values to be used as objects.

One aspect of Java that can cause difficulty for a C++ programmer is that variables whose value is an object actually contain a reference to the object, rather than the object itself. Thus an assignment statement can result in two variables that reference the same object, as in Smalltalk. In this sense, the variables (except for those whose values are primitive types) of Java are more like pointers in C++, and the assignment of one variable to another is similar to the assignment of one pointer to another in C++. (Instead of saying that there are no pointers in Java, some prefer to say that there are only pointers in Java.)

Parameters are passed by value (as in Smalltalk and C), but it is important to remember that for objects (as opposed to primitive values), a reference is passed by value. So if the value of a variable is passed as a parameter, the object that is the value of a variable will remain the value of the variable after the method (function) returns. However, if the object has a method that can be used to change the object's value then the value of the object may be changed in the method.

Java is a lot like C++ in syntax. For instance, the language contains the standard ++ and += operators that so many C/C++ users use. But Java does not allow operator overloading as C++ does. Most of the C/C++ control structures (if, for, while, etc.) are available in Java, and the syntax is mostly the same in Java as in C.

One of the most important aspects of Java is its standard libraries (packages). Java has a package for Threads, another for Networking, and another for Windowing (the AWT). The base (Sun standard) language is platform independent and free!!!

PRIMITIVE TYPES

Java has all the primitive types of C (int, double, float, char, etc.), plus boolean and byte. These are simple data types but can be wrapped inside objects that are instances of classes with names that are similar to the primitive type but with the first letter capitalized (Boolean, Character, Integer, etc.). Another point to remember is that unlike C/C++ there are no char * strings. Instead you have to create String objects.

VARIABLE AND METHOD ACCESS

By default each variable (called a field in Java terminology), and method is accessible only to the other methods of classes in the same source file (package). (That is, all classes in the same file are something like friend classes in C++.) The public keyword allows a method or variable to be accessible from all classes, while the private keyword allows them to be accessible only in the class in which they are declared. The protected keyword has a similar meaning to that in C++, but protected entities are also accessible in all methods of classes in the same package.

In order to have access to the public components of a class in another file, the class must be imported, unless the class is in the same directory in a file that follows the required naming conventions, or the class has been compiled and is in the same directory.

CLASSES IN JAVA

All classes in Java are derived (in C++ terminology) from an existing class except for the class Object, which is at the root of the inheritance hierarchy as in Smalltalk. Java uses the syntax

class B extends A { ... }

instead of the C++

class B : public A { ... };

In C++ terms, all methods in Java are virtual.

PARAMETER PASSING

As indicated above, parameter passing for objects is done by passing a reference to the object, by value. Thus if a variable is passed as an argument and the corresponding parameter is changed by an assignment in the function, the object that is the variable's value will not change. However, if the value of the object is changed in the function, then the caller will also see that change.

The primitive types in Java (int, char, ...) are not objects, and they are passed by value.

HEAP MANAGEMENT

Java has no pointers, and there is no explicit heap allocation by a program (such as new in C++ or malloc in C). However, there is implicit heap allocation whenever a new object is created by the new operator in Java, and logically there is little difference between new in Java and C++. However, there is no analog at all in Java to delete in C++. Java uses garbage collection (which operates unobtrusively as a thread process in the background) to reclaim heap storage that is no longer referenced from the program.

APPLICATIONS

"Normal" programs in the traditional sense are called applications in Java. (This is as opposed to applets, which execute from a web browser.) An application is invoked from the command line or other standard way for a particular platform. The construction of an application is in some ways similar to the construction of a "main" program in C++, except that "main" must be a method of a class. A Java class that contains a method declared as

public static void main (String[] args)

can be used as an application. "Executing" the application causes the main method to be invoked with the command line argument string as its parameter, just as in C/C++.

The method main above is declared static. This designates that it is a class method rather than an instance method (in Smalltalk terminology). The static keyword can be used with any field (variable) or method to make it a class field or method. For a field, this means that there is only one copy of the field that is shared by all instances of the class (or all methods of all classes if it is public -- yes, C hackers, you can still have your global variables if you really insist upon it!). A static method essentially becomes a global function that does not operate on an object, so in this sense it is like a function in C. Note that static methods and fields are accessed by treating them as fields and methods of the class. For example, for the class definition indicated by

class Example {
static public int foo;
static public void bar( ... ) { ... }
. . .

The static members foo and bar could be used as indicated by

Example.foo = 17;
Example.bar( ... );

Note that this capability is not intended to allow C-style code to be written in Java.