• The programs are constructed out of objects - in a simplistic explanation each object is like a small subroutine, the main difference is that the object structure defines a hierarchy of functions, and allows functions to share and hide data and other functions in creative ways.
• Variables in Java are scoped to their declaration, and are static, but objects can be refered to dynamically.
• The Java source is compiled into ‘byte codes’, or a generic machine language, and then stored in ‘.class’ files. The byte codes are then translated into local machine code, with local extras added in.
• The Java applets are architecture neutral, and can be recompiled to other architectures. Machine specific details are considered using libraries. (using the prefix ‘java.’) An author may include none, some, or all the libraries in their programs. Although not consistent with the objectives of the langauge, customized libraries can also be added for special functions.
• Java uses “threads” to allow a program to split into parallel streams of execution. On a simple machine this won’t speed up execution, but will simplify programming.
• In Java there are no pointers, as a result the memory can be rearranged at times (stored variables and arrays can be moved in memory to be more compact. This “garbage collection” process is done automatically.
• Local code written in other languages can be used with the ‘native’ command.
• A simple example of an applet embedded in an HTML document is given (HTML is not the only way to run an applet)
<APPLET code=applet_name.class width=200 height=200>
<PARAM> name=file value=.cshrc>
<PARAM name=read_write value=read>
• Applets can be organized using subdirectories, this is a good idea if there are many pieces. A good idea is have a subdirectory for each class/package. Under these have subdirectories for images, sounds, etc.
• Like most languages Java requires a compiler “javac” and then a separate viewer.
boolean - a 1 bit true or flase value
float - a 4 byte floating point number
double - an 8 byte floating point number
• arrays (Note: No pointers) are defined as shown below, and have bounds checking when used.
int a[10] = {0,1,2,4,8,16,32,64,128,256};
int a[3][4] = {{0,1,2,4},{8,16,32,64},{128,256}};
- comments use ‘/*...*/’, or ‘//’
- statements end in semicolons ‘;’
- curly brackets start and end program blocks ‘{........}’
- ‘if’ statements use boolean arguments (but no others), for example,
- ‘if...else’ statements are allowed
- ‘while’ and ‘do......while’ statements are allowed
- ‘switch’ statements are used
• classes are required for all code,
abstract - for a class that is not directly implemented
final - not allowed to be a subclass
public - the code can be used by other classes outside the package - the file. This class name must also be the name of the file <ClassName>.java
private - can only be used by code in the same file.
<empty> - if no access specifier is given (public or private), the class can be accessed from within the current package.
synchronizable - this function allows arguments that are instances of itself to allow synchronization.
• Functions in classes (Methods) can be modified using,
public - this function can be called by any other.
protected - this function can only be called by subclasses.
private - this function can only be called by peers in the same class.
<blank> - can be called by functions in the same package.
final - no other methods can be used to override.
static - a generally shared function by all subclasses started with <classname>.method_name.
synchronized - a lock will be applied and released on exit to prevent deadlock situations.
native - allows non-Java code to be used (e.g., a ‘C’ function)
• exceptions are one function not found in C/C++ and are intended to help trap errors. These exceptions are generated with ‘throw’ statements when a problem occurs. The results of a throw statement is then considered in another function by a ‘try...catch’ clock,
// executed fully if no exception thrown
// If exception occurs run best match catch
//the best match for the exceptions will be found
// do this wether or not there was an exception
• Recall, for class definitions, there is one function with the same name as the class (called the constructor) that is executed whenever a new instance of the class is created.
• functions may have the same name, but different arguments, this allows function overloading.
• the ‘this’ variable can be used to get a self reference to a function.
• There are no header files to include, but other classes or packages can be added using the ‘import’ function.
java.lang - the basic language classes (automatically imported)
java.io - allows input and output to other devices
java.util - utilities such as hash tables and vectors
java.net - network read/write functions
java.awt - GUI toolkit (e.g. windows)
java.applet - interfaces for WWW or other browsers
• ‘interface’ is a special command that allows inheretance without writing a new class. An interface can also be used to ‘extend’ a class.
• we can define classes that ‘implement’ one or more interfaces.
• Threads allow multiple streams of execution in parallel. Basically, if a program has to do more than one task, the traditional appraoch would be to do one for a while, then the other. But if at some point the program execution can be split (into threads) then execution will continue without paying attention to task switching. To do this a ‘Thread’ is initiated.
• If a class implements ‘Runnable’ it should have a function called run (needed for threads among other things).
• If running parallel threads, and they share common resources/data, we might take precautions to ensure that the two don’t read/write at the same time. We can do this by declaring potentially conflicting functions as ‘synchronized’. Care must be taken to avoid deadlock when a ‘synchronized’ function causes a monitor to lock a device/data neededby another ‘synchronized’ function. (these two or more functions would have to try to lock two or more resources).