Professional Documents
Culture Documents
• Bill Joy
• Retargeted as the Internet language (‘94-95) – ABD from Berkeley (Unix)
– Every new language needs a “killer app”
– Beat out TCL, Python • Guy Steele
– ActiveX came later – MIT PhD
– Famous languages researcher
• Lisp
• Java is a BIG language
– Java’s dynamic flavor (lots of features)
– Lots of features
– Lots of feature interactions
Prof. Aiken CS 143 Lecture 18 5 Prof. Aiken CS 143 Lecture 18 6
1
Arrays Subtyping In Java
• Java fixes the problem by checking each array • Deep in a section of code, you encounter an
assignment at runtime for type correctness unexpected error
– Is the type of the object being assigned – Out of memory
compatible with the type of the array? – A list that is supposed to be sorted is not
– etc.
• Huge overhead on array computations!
• What do you do?
• But note: arrays of primitive types unaffected
– Primitive types are not classes
2
Exceptions Example
3
Type Checking Interfaces
• Methods must declare types of exceptions • Specify relationships between classes without
they may raise inheritance
public void X() throws MyException
– Checked at compile time
interface PointInterface { void move(int dx, int dy); }
– Some exceptions need not be part of the method signature
• e.g., dereferencing null
class Point implements PointInterface {
• Other mundane type rules void move(int dx, int dy) { … }
– throw must be applied to an object of type Exception }
“Java programs can use interfaces to make it • A graduate student may be both an University
unnecessary for related classes to share a common employee and a student
abstract superclass or to add methods to Object.”
• Methods in classes implementing interfaces need not • Dispatches e.f(…) where e has an interface
be at fixed offsets. type are more complex than usual
– Because methods don’t live at fixed offsets
interface PointInterface { void move(int dx, int dy); }
• One approach:
class Point implements PointInterface {
– Each class implementing an interface has a lookup
void move(int dx, int dy) { … } } table method names methods
class Point2 implements PointInterface { – Hash method names for faster lookup
void dummy() { … } • hashes computed at compile time
void move(int dx, int dy) { … } }
Prof. Aiken CS 143 Lecture 18 23 Prof. Aiken CS 143 Lecture 18 24
4
Coercions Coercions & Casts
• Java allows primitive types to be coerced in • Java distinguishes two kinds of coercions &
certain contexts. casts:
– Widening always succeed (int float)
• In 1 + 2.0, the int 1 is widened to a float 1.0 – Narrowing may fail if data can’t be converted to
desired type (float int, downcasts)
B = ‘123’
What is the only type in Java for which there C = ‘456’
are no coercions/casts defined? A=B+C
• What is A?
5
Example (Cont.) Example (Cont.)
• Even without synchronization, a variable • Java allows classes to be loaded at run time
should only hold values written by some thread – Type checking source takes place at compile time
– Writes of values are atomic – Bytecode verification takes place at run time
– Violated for doubles, though
• Loading policies handle by a ClassLoader
• Java concurrency semantics are difficult to
understand in detail, particularly as to how • Classes may also be unloaded
they might be implemented on certain
– But poorly specified in the definition
machines
• Initialization in Java is baroque 1. Lock the class object for the class
– Everything in COOL plus much more • Wait on the lock if another thread has locked it
– Greatly complicated by concurrency 2. If the same thread is already initializing this
class, release lock and return
• A class is initialized when a symbol in the class 3. If class already initialized, return normally
is first used 4. Otherwise, mark initialization as in progress
– Not when the class is loaded by this thread and unlock class
– Delays initialization errors to a predictable point
(when something in the class is referenced)
6
Class Initialization (Cont.) Features and Feature Interactions
5. Initialize superclass, fields (in textual order) • In any system with N features, there are
• But initialize static, final fields first potentially N2 feature interactions.
• Give every field a default value before
initialization
• Big, featureful systems are hard to
6. Any errors result in an incorrectly initialized understand!
class, mark class as erroneous – Including programming languages
7. If no errors, lock class, label class as
initialized, notify threads waiting on class
object, unlock class
Summary