Lecture 018

Java

Histroy

Java is begin as Oak at SUN. Designed for coding set-top device for TV. Project failed. But Internet happened after, people want secure language, unlike binaries in C. Every language needs a "killer app". For Java, it was Internet.

Java's influence: Modula-3 type, Eiffel, Object C, C++ objects and interface, Lisp dynamic flavor.

Arrays

Java supports aliasing of object class within array, creating aliasing problem

Java supports aliasing of object class within array, creating aliasing problem

(ie. write as parent class, read as child class that has missing fields)

Java type checks at runtime, which is slow.

Other good typing system have separate type system for arrays

Other good typing system have separate type system for arrays

Exceptions

Execution rule for exception

Execution rule for exception

Throw and Propagate for exception

Throw and Propagate for exception

Exception object is not an ordinary object.

Exception Implementation:

  1. when we encounter try, mark current location in stack
  2. when throw, throw away stack until first try and execute corresponding catch

It introduce some overhead during normal execution. But more complex techniques reduce overhead. (and by make exception slightly more expensive for trade off)

finalization: before Java garbage collect an object, it will call the object's finalization method to do some user specified cleanup work. (useful for closing file handler)

When there is exception in finalization method, should garbage collector throw exception and abort? No. The exception is dropped like nothing happen.

Since many programmer forget to handel exception, Java's compiler check for possible exception and always enforce programmer to handel them. (except for some common exceptions like integer overflow)

Interfaces

Interface: specify relationship between classes without inheritance. (must provide same signature, and one class can implements multiple interfaces)

It is harder to implement interfaces than inheritance, since methods in interfaces need not be at fixed offset. (infinitely many combination fo possible interfaces since there exists no total ordering)

One inefficient approach: lookup table from method string name to method address.

Coercions

Coercion: compiler cast type for you.

bool is the only type that has no coercion defined

Threads

Thread: has own program counter, own stack

Synchronization: obtain a lock on object so only one thread at a time can access.

Java guarantees write of values are atomic except for double (since it is two machine instruction, unless is marked as volatile).

Concurrency in language is still in research.

Dynamic Loading

Java allows classes to be loaded at run-time.

This is troublesome: type checking happens at compile time. So bytecode verification is needed when the class is loaded at run-time. Policies handled by ClassLoader

Classes can be unloaded, but behavior is not well-specified.

Initialization

Class initialization in java: class is initialized when it is first used, not when loaded. (this is to make error in initialization predictable)

Initialization: we are talking about THE class, not an instance of the class (used for introspection and reflection)

  1. lock the class object so only one thread is looking at it
  2. if already initialized (or in the process of initializing by other threads), release lock and return
  3. otherwise, mark initialization in progress and unlock class
  4. initialize superclass, fields (textual order, but static, final fields first with default value)
  5. if error, mark class as erroneous
  6. if no error, lock class, label as initialized, notify other treads waiting on class object to be initialized, unlock class

lesson: any system with N features, there is around N^2 feature interactions (assuming pairwise feature), subset feature will grow exponentially.

Summary

Java was successful by production language standard

Table of Content