Basic Java - Slides - Programming Language Technology and Paradigms | CMSC 433, Study Guides, Projects, Research of Programming Languages

Material Type: Project; Professor: Sussman; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Spring 2001;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 02/13/2009

koofers-user-akh
koofers-user-akh 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 433, Alan Sussman, U. Maryland (via
Bill Pugh) 1
CMSC433, Spring 2001
Programming Language
Technology and Paradigms
Basic Java, continued
Alan Sussman
February 12, 2002
CMCS 433, Spring 2002 -Alan Sussman 2
Administrivia
Project 1 due Wednesday, 6PM
threads issue
anyone not get email to reflector?
projects for commentary emailed after late
deadline
Second Java project posted soon
CMCS 433, Spring 2002 -Alan Sussman 3
Last time -Java
Instance variable/method modifiers
both: public, protected, package, private, static
method: abstract, final, native, synchronized
field: transient, volatile, final
Overriding/Overloading
Constructors
call another constructor or super() first
Static methods/instance variables
associated with Class object
Class object used for reflection operations
Interfaces are types
CMCS 433, Spring 2002 -Alan Sussman 4
Types
A type describes a set of values that can be:
held in a variable
returned in an expression
Types include:
primitive types boolean, char, short, int , …
Reference types:
Class types
Array types
Interface types
CMCS 433, Spring 2002 -Alan Sussman 5
Class types
Using the name of a class as a type means a
reference to an instance of that class or a
subclass is a permitted value
a subclass has all the fields of its superclass
a subclass has all the methods of its superclass
null is also an allowed value
CMCS 433, Spring 2002 -Alan Sussman 6
Array types
If Sis a subtype of T
S[] is a subtype of T[]
Object[] is a supertype of all arrays of reference
types
Storing into an array generates a run-time check
that the type stored is a subtype of the declared
type of the array elements
Performance penalty?
Similar (and maybe worse) problems in C++
pf3

Partial preview of the text

Download Basic Java - Slides - Programming Language Technology and Paradigms | CMSC 433 and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

CMSC 433, Alan Sussman, U. Maryland (via

CMSC433, Spring 2001

Programming Language

Technology and Paradigms

Basic Java, continued

Alan Sussman

February 12, 2002

CMCS 433, Spring 2002 - Alan Sussman 2

Administrivia

• Project 1 due Wednesday, 6PM

  • threads issue
  • anyone not get email to reflector?
  • projects for commentary emailed after late deadline

• Second Java project posted soon

CMCS 433, Spring 2002 - Alan Sussman 3

Last time - Java

  • Instance variable/method modifiers
    • both: public, protected, package, private, static
    • method: abstract, final, native, synchronized
    • field: transient, volatile, final
  • Overriding/Overloading
  • Constructors
    • call another constructor or super() first
  • Static methods/instance variables
    • associated with Class object
    • Class object used for reflection operations
  • Interfaces are types CMCS 433, Spring 2002 - Alan Sussman 4

Types

• A type describes a set of values that can be:

  • held in a variable
  • returned in an expression

• Types include:

  • primitive types – boolean, char, short, int , …
  • Reference types:
    • Class types
    • Array types
    • Interface types

CMCS 433, Spring 2002 - Alan Sussman 5

Class types

• Using the name of a class as a type means a

reference to an instance of that class or a

subclass is a permitted value

  • a subclass has all the fields of its superclass
  • a subclass has all the methods of its superclass

• null is also an allowed value

CMCS 433, Spring 2002 - Alan Sussman 6

Array types

  • If S is a subtype of T
    • S[] is a subtype of T[]
  • Object[] is a supertype of all arrays of reference types
  • Storing into an array generates a run-time check that the type stored is a subtype of the declared type of the array elements
  • Performance penalty?
  • Similar (and maybe worse) problems in C++

CMSC 433, Alan Sussman, U. Maryland (via

CMCS 433, Spring 2002 - Alan Sussman 7

Example: Object[]

public class TestArrayTypes { public static void reverseArray(Object [] A) { for(int i=0, j=A.length-1; i<j; i++,j --) { Object tmp = A[i]; A[i] = A[j]; A[j] = tmp ; } } public static void main(String [] args) { reverseArray(args); for(int i=0; i < A.length; i++) System.out.println(args[i]); } } (^) CMCS 433, Spring 2002 - Alan Sussman 8

Interface types

• Using the name of an interface as a type

means

  • a reference to any instance of a class that implements the interface is a permitted value
  • null is also allowed

• Object referenced is guaranteed to support

all the methods of the interface

  • invoking a method on an interface might be a bit less efficient

CMCS 433, Spring 2002 - Alan Sussman 9

Object Obligations

  • many operations have default implementations
    • which may not be the ones you want public boolean equals (Object that) { … } // return this == that public String toString() { … } // returns print representation public int hashCode() { … } // key for accessing object // important that a.equals(b) implies a.hashCode()==b.hashCode() public void finalize() { … } // called before object garbage // collected, default is {} public Object clone() { … } // default is shallow bit -copy if class // implements Cloneable, throw CloneNotSupportedException // otherwise

CMCS 433, Spring 2002 - Alan Sussman 10

Poor man’s polymorphism

  • Every object is an Object
  • An Object[] can hold references to any objects
  • E.g., for a data structure Set that holds a set of Object - can use it for a set of String - or a set of images - or a set of anything
  • Java’s container classes are all containers of Object - when you get a value out, have to downcast it

Interacting with External

Environment

CMCS 433, Spring 2002 - Alan Sussman 12

Applications and I/O

  • Java external interface is a public class
  • via public static void main(String [] args)
  • args[0] is first argument
    • unlike C/C++
  • System.out and System.err are PrintStreams ’s
    • should be PrintWriter ’ s, but would break 1.0 code
    • System.out.print(…) prints a string
    • System.out.println(…) prints a string with a newline
  • System.in is an InputStream
    • not quite so easy to use