Title: An Overview of the Java Programming Language
1An Overview of the Java Programming Language
-
- Zhong Shao
- Department of Computer Science
- Yale University
- September 15, 1999
2Reading list
- Required
- LY99 Chap 2 Java PL Concepts (handout also
online) - Bill Pughs Java crash course slides (on the web)
- References
- AG97 The Java Programming Language (2nd Edition)
- GJS96 The Java Language Specification (online)
- Java spec report (a web site discussing dirty
issues in Java) - Java programmers FAQ (available online)
- If you are unfamiliar with Java
- read AG97 or David Flanagans Java in a
Nutshell and Java Examples in a Nutshell or
Pughs Learning Java web site.
3Objectives
- A thorough understanding of important Java
features - Focus on their specifications rules rather than
how to program with them - Goal create a Java language specification
cheat-sheet - Comparison with other languages (ML, Haskell, C,
C) - what a modern programming language should support
? - what are Javas strong points?
- what are Javas weak aspects?
- Create a list of topics that we should revisit
- Create a list of research problems on the Java
language design and extension
4What I will cover ?
- Big picture
- Java basics
- unicode types, values, and variables
- conversions and promotions
- names and packages
- OO support
- classes interfaces constructors methods
fields initializers - the (modifiers x features) matrix
- how to program with interfaces?
- Arrays and exceptions
- Execution basics (if time permits)
- Java reflection (if time permits)
5What I will NOT cover ?
- Statements and expressions syntax (Oct 19, 26)
- Inner classes (probably a future topic)
- Execution detail (what this whole seminar is
about) - Concurrency features (Nov 3)
- Documentation comments
- Java coding conventions
- Java class libraries
- Binary compatibility (Dec 8)
6The big picture
- What is in the JDK ?
- The src-to-bytecode compiler (javac)
- The java virtual machine (java)
- Tools jdb, jar, javap, javah, appletviewer,
javadoc, ... - The class libraries (a file named rt.jar)
- The native libraries (.so files used by jre ?)
- Not resolved
- The .h files in the include and include.old
directories - must be related to writing JNI C programs
- What is the difference between JDK and JRE ?
(see FAQ)
7The Java class libraries (v1.2 std)
- Java (36 packages)
- applet (applet creation
communication w. the context) - awt(12) (the abstract window toolkit
the gui stuff) - beans(2) (java components)
- io (data streams object
serialization file system) - lang(3) (things in the Java language
itself) - math (bignum mathematics)
- net (URL, socket, networking
applications) - rmi (5) (remote method invocation)
- security(5) (security tools access control
code signing ...) - sql (relational database
access) - text (internationalization
localization) - util(3) (useful data structures,
random number, jar/zip) - javax (17) (accessibility swing)
why x ? - org.omg (7) (CORBA CosNaming)
8The Java class libraries (extra)
- Also came with the JDK 1.2.2 release (in rt.jar)
- com.sun
- CORBA
- CosNaming
- image.codec.jpeg
- java.swing.plaf.motif java.swing.plaf.windows
- sun
- lots of classes for applet, audio, awt, beans,
dc, io, java2d, jdbc, misc, net, security, tools
- are they machine-dependent parts of the Library ?
Where are their sources ? - sunw (for backward compatibility with JDK 1.0.2)
- io (Serializable.class)
- util (EventListener.class EventObject.class)
9What is in java.lang (v1.2) ?
- Java.lang (75 classes 3 interfaces)
- Object Class (2)
- String StringBuffer Math(3)
- Wrapper classes for primitive types (12)
- ClassLoader Compiler Package Process
Runtime System (6) - SecurityManager RuntimePermission (2)
- Thread-related classes (4)
- Throwable 21 errors 24 exceptions (46)
- interfaces Cloneable Comparable Runnable (3)
- Java.lang.reflect (8 classes 1 interface)
- AccessibleObject (Constructor Field Method) /
Member Array Modifier ReflectPermission
InvocationTargetException - Java.lang.ref (5 classes)
- pointer to objects weak pointers ...
10What is 100-pure Java ?
- Checkout the cookbook on the web roughly,
- Contain no native methods
- No use of undocumented parts of a Java
implementation - No use of tunnel methods (no access to machine
code) - No use of hardwired platform-specifc constants
11The Java programming language
- The basics
- unicode, identifiers, literals, types, values,
variables - initialization of local variables (definite
assignment) - names, packages
- conversions and promotions
- statement and expressions (not covered)
- OO features
- classes and interfaces w. modifiers
- methods, fields, constructors, and initializers
w. modifiers - inner classes and interfaces (not covered)
- field shadowing method overloading, overriding
implementing - Arrays
- Exceptions
- Classes String Object Class
12The basics lexical structure
- Unicode (16 bits char rather than 8 bits)
- used for comments, identifiers, character and
string literals - the rest are in ASCII (but what are the rest?)
- Identifiers
- Letter followed by sequence of letters and digits
- Not keywords and not true and false
- Literals
- ints (1, -1) hex ints (0x0f28) unicode hex
(\u003f) - A leading 0 on an integer is an octal (027) ---
potential bug - longs (3L, -99l, 0xf011223344L)
- floats (1.0345F, 1.04E-12f, .0345f, 1.04e-13f,
Float.NaN) - doubles (5.6E-120D, 123.4d, 0.1, Double.NaN,
Math.PI) - Floating point literals without trailing f, F, d
or D are considered double.
13More Java literals
- null type literal (null)
- boolean (true and false)
- strings ("ABC)
- chars 'A', or integer forms e.g. 45, 0x45,
'\u003f' - Escape sequences inside char and string literals
include - '\u003f' unicode hex, (must be exactly 4 digits)
- '\n' newline '\b' backspace '\f' formfeed
- '\r' carriage return '\t' tab '\\' backslash,
- '\'' single quote (optional inside " ")
- '\"' double quote (optional inside ' ')
- '\377' octal (must be exactly 3 digits)
- \007 bel \010 backspace
- \013 vt vertical tab
- \032 sub, eof
14Types and values
- primitive types
- boolean
- numeric types
- signed integral types
- byte (8 bits) short (16 bits) int (32 bits)
long (64 bits) - char (16 bits unsigned)
- floating-point types (IEEE 754)
- float (32 bits) and double (64 bits)
- what are the floating-point value sets ?
- void ( ? )
- reference types
- class types
- interface types
- array types
- null (subtype of any reference types)
15Special types in java.lang
- String
- used to type the string literals
- Object
- root class
- Thread
- interaction with certain modifiers
- Class
- for reflection
- restrict the implementation
16Variables
- A variable has type (meaning compile-time type)
- always stores a value assignment-compatible with
the type - initialized with default values
- checked at each assignment
- no variable of type void
- Seven kinds of variables in Java
- a class variable (i.e., static field) --- gone if
class unloaded - an instance variable --- die when the object is
finalized - array components (un-named) --- die when the
array is gc-ed - method parameters
- constructor parameters
- an exception-handler parameter (always an
Throwable object) - local variables
17Variable initialization
- Class variable, instance variable, array
components, if not initialized, are given
default values - 0, 0L, 0.0f, 0.0, \u0000, false, null
- Initialization blocks (member of the class)
- static --- executed when the class is loaded
- non-static --- executed during object creation
(before the constructor is run) - order is significant mutual recursion is unclear
? -
- Local variables (definite assignment)
- see chapter 16 of the Java Language Specification
- look through the control flow paths and check if
the variable is assigned on every path
18Variables have types, objects have classes
- Every variable has a compile-time type --- type
- Every object has a runtime type ---- class
- the class used in the new expression
- the class whose class object was used to invoke
the newInstance method - arrays have strangely named classes ( I for
array of int) - compile-time type limits the runtime class
19Conversions and promotions
- Six broad kinds of conversions
- identity
- widening primitive
- narrowing primitive
- widening reference (also w. arrays)
- narrowing reference (also w. arrays)
- string conversion (toString)
- Five conversion contexts
- assignment conversion
- no exceptions (widening int-const-narrowing)
- method/constructor invocation conversion (on
arguments) - widening only
- casting conversion
- string conversion (toString)
- numeric promotion (arguments of numerical
operators)
20Names and packages
- Names refer to entities declared in a program
- package, type, field and method of a type,
parameter, or local variables - simple name (identifier)
- qualified name (name.identifier)
- packages
- subpackages
- class types
- fields, methods, nested classes and interfaces
(constructors?) - interface types (same)
- array types (those in class Object the length
field) - fully qualified name (for all packages and types)
- array types name the component type name
21The OO features
- Class (ext. super-class imp. super-interfaces)
- field
- method
- constructor
- initialization block
- inner class
- inner interface
- Interface (ext. super-interfaces)
- field
- method
- ? no inner interface ?
22Java modifiers (10 2)
- public gt protected gt package (default) gt private
- final vs. non-final (default) - for
mutability/extensibility - abstract vs. non-abstract (default )
- static vs. non-static (default)
- native vs. non-native(default) - for
foreign-function call - synchronized vs. non-synchronized (default)
- volatile vs. non-volatile(default)
- transient vs. non-transient(default) - for
persistence - strictfp and widefp
23Features vs. modifiers
- Class (public, abstract, final)
- field (pub-prot-priv, static,
final, volatile, transient) - method (pub-prot-priv, static, final,
abs, sync, native) - constructor (pub-prot-priv)
- initializer (static)
- inner class (pub-prot-priv, static, final,
abs) - inner interface (public)
- Interface (public)
- field (pub-prot-priv ?, static,
final) - implicitly static, final, public so no package,
volatile, transient - method (pub-prot-priv ?, abs)
- implicitly abstract, non-static
- information about final, native, sync., strictfp
is lost
24Subtlety with modifiers
- What are the conflicting pairs of modifiers ?
- abstract class cannot be final
- final field cannot be volatile
- native method cannot be strictfp
- abstract method cannot be priv., static, final,
native, strictfp - Methods in a final class are implicitly final.
- Private methods are implicitly final.
- Static methods can not directly call and refer to
non-static methods and fields of the same class
25A class extends a super-class
- Overloading methods (w. different signatures)
- method signature method name types of formal
params - Overriding/hiding methods (w. same signatures)
- return type must be same
- the throw clauses must be non-conflicting
- method n overrides/hides method m if for each
exn type in the throws clause of n, the same
exn class or one of its super-classes must occur
in the throws clause of m. - overriding private methods dont have to satisfy
these rules - must have as much as access as the original
method - other modifiers static/final/native/abstract/sync
hronized? - Hiding fields (w. different or same types)
26Hiding fields
- Can a class/interface defines two fields with
same name ? -
- Fields are accessed based on the compile-time
type of the underlying object
27Hiding fields example
- class SuperShow
- public String str SuperStr
- public void show() System.out.println(Super.s
how str) -
- class ExtendShow extends SuperShow
- public String str ExtendStr
- public void show() System.out.println(Extend.
show str) - public static void main(String args)
- ExtendShow ext new ExtendShow()
- SuperShow sup ext
- sup.show()
- ext.show()
- System.out.println(sup.str sup.str)
- System.out.println(ext.str ext.str)
-
28Implementing and extending interfaces
- Same requirements as before
- Can inherit multiple methods with same signatures
- all abstract methods (compatible pairwise)
- all abstract methods except one (that one
implements all the other abstract methods) - Inheriting multiple fields
- compile-time error if any ambiguity arises.
29Implementing constructors
- Deafult constructor
- for Object, the empty body
- for others, calls super()
- same access as the class
- Abstract class cant have constructor
- Except for Object, each constructor body starts
with an explicit constructor invocation or just
super()
30Method invocation
- Invoking static methods
- through class name
- through super
- through object or through this (only its static
type matters) - Invoking non-static methods
- through super
- through this
- through object (only its dynamic class matters)
31Arrays
- Language built-in class generator, not in
java.lang - array classes are hardwired as a sub-class of
Object - inherit everything in Object
- override the clone method
- implements Cloneable and java.io.Serializable
- add an additional final length field
- array of char is not string string is immutable
32Exceptions
- Throwable
- Error (unchecked)
- LinkageError
- VirtualMachineError
- other unchecked exn classes
- Exception
- RuntimeException (unchecked)
- ArithmeticException
- ArrayStoreException
- ClassCastException
- NullPointerException
- other unchecked exn classes
- ClassNotFoundException
- IllegalAccessException
- other checked exn classes
- Other checked exception classes
33More on Exceptions
- Try-catch-finally
- Try-catch
34Executions
- VM starting up
- class and interface type loading, linking,
initialization - creation
- finalization
- unloading
- exit VM
35Things I want to find out
- Method invocation and field access on the null
value? - Method overriding in the presence of the throw
clause? - How the throw clause is checked?
- What are marker interfaces?
- How the Array class differs from other classes
- The Java exception hierarchy is it much better
than exceptions in ML or Modula-3 ? - How does the initializer work ?
36Potential topics for discussion
- What features in Java we really like and what we
really hate? - Comparison with other programming languages
- What a modern prog. language should support ?
- What are interesting language extensions over
Java?