Java - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Java

Description:

But Java Still has omissions: No operator overloading (syntactic annoyance) ... ClassLoader controls which classes have which permissions. 20. Declarative Programming ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 26
Provided by: walt185
Learn more at: https://cs.nyu.edu
Category:
Tags: java

less

Transcript and Presenter's Notes

Title: Java


1
Java
  • Object oriented features
  • Polymorphic type
  • Inheritance
  • Information Hiding
  • Modules
  • 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

2
  • The Java Programming Language

3
Why Java?
  • Originally developed at Sun for embedded consumer
    devices.
  • Cable TV set-top boxes, etc.
  • Developed by James Gosling
  • Code distributed to devices via a wide area
    network.
  • 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

4
How is Java different from other languages
  • Less than you think
  • Java is an imperative language (like C, Ada, C,
    Pascal)
  • Java is interpreted byte code (like LISP, APL)
  • Java is garbage-collected (like LISP, Eiffel,
    Modula-3)
  • Java can be compiled (like LISP)
  • Java 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-real-time applications

5
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 cant really be simple!

6
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

7
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

8
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)

9
JavaDoc
  • Automatically generates user documentation
  • Special comments containing keywords
  • / ? this introduces a special comment
  • _at_author Guy Steele
  • /
  • Keywords
  • _at_return return value
  • _at_param parameter
  • _at_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.

10
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)

11
Interface Comparable
  • public interface Comparable
  • public int CompareTo (Object x) throws
    ClassCastException
  • // returns -1 if this lt x,
  • // 0 if this x,
  • // 1 if this gt x
  • Implementation has to cast x to the proper class.
  • Any class that may appear in a container should
    implement Comparable

12
Interfaces and event-driven programming
  • A high-level model of event-handling
  • graphic objects generate events
  • mouse click, menu selection, window close...
  • an object can be designated as a handler
  • a listener, in Java terminology
  • an event can be broadcast to several handlers
  • several listeners can be attached to a source of
    events
  • a handler must implement an interface
  • actionPerformed, keyPressed, mouseExited..

13
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(cant be
    that bad. Whats the problem?)

14
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.

15
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

16
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 Javas security system

17
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

18
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 ( )
    // is... predicate
  • public int getRoast ( ) // get...
    retrieval
  • public void setRoast (int darkness)
    // set assignment
  • getBeanInfo(Class.fromName(CoffeeBean))
  • Returns a BeanInfo object

19
Security
  • Essential for distributing code over the internet
  • Untrusted code ran in Sandbox on original Java
  • Now Java has fine-grained security system
  • Restricts access to system resources
  • reading or writing files, system exit, etc.
  • Based on a Policy that grants permissions
  • 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

20
  • Declarative Programming

21
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

22
Horn Clauses
  • A Horn clause is of the form
  • 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 0..n. then C is true
  • If C ? A, B and D ? C then D ? A, B
  • Terms are usually parameterized predicates
  • Rainy(Seattle)
  • True iff it rains in Seattle
  • If flowers(X) ? rainy(X) and rainy(NY) then
    flowers(NY)

Sta
23
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

24
A Prolog Program
  • A Program is a sequence of statements
  • rainy(seattle)
  • rainy(rochester)
  • cold(rochester)
  • snowy(x) - rainy(X), cold(X)
  • A Query is applied to the program
  • ?- snowy(C)
  • C rochester
  • Interpreter may return only the first or all
    values satisfying the query

25
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
Write a Comment
User Comments (0)
About PowerShow.com