Download Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java

Object oriented features




Modules




Polymorphic type
Inheritance
Information Hiding
Interface vs. Implementation
Separate Compilation
Security
Software Engineering





Maintenance – No global variables, type checks
High level functionality – Well defined Framework
Documentation – Automatic
Organization/Modules -Interfaces, contracts, and implementation

1
The Java Programming
Language
2
Why Java?

Originally developed at Sun for embedded consumer
devices.



Code distributed to devices via a wide area network.




Cable TV set-top boxes, etc.
Developed by James Gosling
Many different kinds of devices
Security
Reliability
Popularity of WWW in early 1990s



Sun retargets Oak as a general programming language
Renamed to Java
Applets for the Web pages
3
How is Java different from other languages

Less than you think:






Java
Java
Java
Java
Java
is an imperative language (like C++, Ada, C, Pascal)
is interpreted byte code (like LISP, APL)
is garbage-collected (like LISP, Eiffel, Modula-3)
can be compiled (like LISP)
is object-oriented (like C++, Ada, Eiffel)
But introduces important new features



Java includes documentation standards in language
Java includes security features
Java includes reflection to reveal meta-information

A successful hybrid for a specific-application domain

A reasonable general-purpose language for non-realtime applications
4
Original design goals (white paper 1993)








Simple
(easy to learn and use)
Object-oriented (inheritance, polymorphism)
Distributed
(intended for networks)
Interpreted
(by Java Virtual Machine, JVM)
Multithreaded
(built-in synchronization primitive)
Robust
(strongly typed)
Secure
(run-time security checks)
Architecture-neutral (JVM for many platforms)
A language with threads, objects, exceptions and
garbage-collection can’t really be simple!
5
Portability



Critical concern: write once-run everywhere
Intended for distribution over wide area network
Consequences:






Portable interpreter
definition through virtual machine: the JVM
run-time representation has high-level semantics
supports dynamic loading
(+) high-level representation can be queried at run-time to
provide reflection
(-) Dynamic features make it hard to fully compile, safety
requires numerous run-time checks
6
Contrast with conventional systems languages
(C, C++, Ada)

Most Conventional languages are fully compiled:






run-time structure is machine language (not portable)
minimal or no run-time type information
language provides low-level tools for accessing storage
safety requires fewer run-time checks because compiler
(least for Ada and somewhat for C++) can verify correctness
statically.
Languages require static binding, run-time image cannot be
easily modified
Different compilers may create portability problems
7
Language Changes

Improvements since Java was introduced in 1995






Event Model
Nested Classes
Parameterized classes (C++ templates, Ada generics)
in Java 1.5 (Beta)
Many Java Technologies (JavaBeans, Telephony, …)
(Distinction between libraries and language is
diminishing)
But Java Still has omissions:


No operator overloading (syntactic annoyance)
No enumeration types (using final constants is clumsy)
8
JavaDoc

Automatically generates user documentation

Special comments containing keywords




Keywords








/**  this introduces a special comment
** @author Guy Steele
**/
@return – return value
@param – parameter
@throws – describes an exception that method throws
Comment associated with class or method immediately following
the comment
Can include HTML tags
Makes it easier to document code
Helps keep documentation current
Extensible: record rationale, maintenance instructions, etc.
9
Interfaces




Separate specification from implementation
Allow related classes to satisfy a given
requirement
Orthogonal to inheritance
 inheritance: an A is-a B (has the attributes of
a B, and possibly others)
 interface:
an A can-do X (and other
unrelated actions)
 better model for multiple inheritance
More costly at run-time (minor consideration)
10
Interface Comparable
public interface Comparable {
public int CompareTo (Object x) throws
ClassCastException;
// returns -1 if this < x,
//
0 if this = x,
//
1 if this > x
};
 Implementation has to cast x to the proper class.
 Any class that may appear in a container should
implement Comparable
11
Interfaces and event-driven programming

A high-level model of event-handling:
 graphic objects generate events


an object can be designated as a handler


a listener, in Java terminology
an event can be broadcast to several handlers


mouse click, menu selection, window close...
several listeners can be attached to a source of events
a handler must implement an interface
 actionPerformed, keyPressed, mouseExited..
12
Events and listeners
class Calm_Down extends Jframe {
private Jbutton help := new Jbutton (“HELP!!!”);
// indicate that the current frame handles button clicks
help.addActionListener (this);
}
// if the button is clicked the frame executes the following:
public void actionPerformed (ActionEvent e) {
if (e.getSource () == help) {
System.out.println(“can’t be that bad. What’s the problem?”);
}
}
13
Introspection, Reflection, and
Typeless programming



public void DoSomething (Object thing) {
// what can be do with a generic object?
if (thing instanceof gizmo) {
// we know the methods in class Gizmo
….
Instanceof requires an accessible run-time descriptor in the object.
Reflection is a general programming model that relies on run-time
representations of aspects of the computation that are usually not
available to the programmer.
More common in Smalltalk and LISP.
14
Separate Compilation

Compilers need to know type of objects defined
elsewhere at compile time



Ada separates spec and body
C and C++ use header files
Java constructs class files which contain:




Method descriptions, including method signatures and Byte code
A constant pool with names and constant values
Reflection information
Package structure related to


Network implementation
File system
15
Reflection and
Metaprogramming

Given an object at run-time, it is possible to obtain:





its class
its field names (data members) as strings
the classes (types) of its fields
the method names of its class, as strings
the types of the methods

It is then possible to construct calls to these methods
This is possible because the JVM class file provides a
high-level representation of a class, with embedded
strings that allow almost complete disassembly.

Protected by Java’s security system

16
Reflection classes

Java.lang.Class
Class.forName(className) // creates a Class object
Class.getMethods () // returns array of method objects
Class.getConstructor (Class[] parameterTypes)
//returns the constructor with those parameters
Class.isInterface() // true if the “class” is an interface

Reflection Classes


Allows access to objects at run-time
Java Beans


High level components
Communicate with events
17
Reflection and beans


The beans technology requires run-time
examination of foreign objects, in order to build
dynamically a usable interface for them.
Class Introspector builds a method dictionary
based on simple naming conventions:
public boolean isCoffeeBean ( );
public int getRoast ( );
public void setRoast (int darkness) ;


// is... predicate
// get... retrieval
// set… assignment
getBeanInfo(Class.fromName(“CoffeeBean”));
Returns a BeanInfo object
18
Security

Essential for distributing code over the internet



Restricts access to system resources


reading or writing files, system exit, etc.
Based on a Policy that grants permissions




Untrusted code ran in “Sandbox” on original Java
Now Java has fine-grained security system
Code base (e.g. a URL)
Signer (names a set of public keys)
Permissions (e.g. reading files)
ClassLoader controls which classes have which
permissions
19
Declarative
Programming
20
Prolog

Logic Programming



Theorem Proving
Program is a series of statements (axioms)
and a goal
Execution attempts to prove goal
Reach goal from axioms
 Uses inference

21
Horn Clauses

A Horn clause is of the form






If C  A, B and D  C then D  A, B
Terms are usually parameterized predicates



C  B1, B2, B3, …, Bn-1, Bn
C is the Head or Consequent
Bi is an term of the Body
When Bi is true for all i Sta
= 0..n. then C is true
Rainy(Seattle)
True iff it rains in Seattle
If flowers(X)  rainy(X) and rainy(NY) then
flowers(NY)
22
Unification

Process of substituting expressions for
variables



A constant unifies only with itself
Two structures unify iff predicate is the same,
the number of parameters is the same and
corresponding parameters unify (recursively)
Variables unify with:
Values – variable instantiated with value
 Other Variables – names alias each other


Unification also used in ML type inference
23
A Prolog Program

A Program is a sequence of statements





A Query is applied to the program



rainy(seattle)
rainy(rochester)
cold(rochester)
snowy(x) :- rainy(X), cold(X)
?- snowy(C)
C = rochester
Interpreter may return only the first or all
values satisfying the query
24
Applications

Limitations


Unification can take exponential time
Subset of first order logic


not is inability to prove, not false
Can be used for





Problems for which efficient and straightforward
procedural solutions are not available
Natural Language
AI and Expert Systems
Relationships (student of, ancestor of)
Relational Database-like applications
25