Lecture Slides on C++project and Java Project | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Professor: Sussman; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-aos
koofers-user-aos 🇺🇸

10 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
October 2, 2001
CMCS 433, Fall 2001 -Alan Sussman 2
Administrivia
C++ project
private drivers posted soon, grades by end of week
comments on other students projects due 10/9
submit as project 11 (e.g. submit 11 1.txt 2.txt)
you will get the comments back for your project
Java project
default compiler is still 1.2, use the one in
/usr/opt/java130/bin for 1.3
try using jikes for compiling, in ~pugh/bin
faster than javac
CMCS 433, Fall 2001 -Alan Sussman 3
Administrivia (cont.)
Exam next Thursday, Oct. 11
practice exam on web page by tomorrow, talk about it
on Tuesday
Guest lecturer on Thursday Bill Pugh
he can answer any Java question, and probably most
questions about project, but see TA or Dr. Porter for
detailed project questions on Thursday or Friday
A warning
will be checking projects against each other for
similarities (with a program)
and against similar projects from previous semesters
CMCS 433, Fall 2001 -Alan Sussman 4
Last time -Java
Overriding methods dynamic dispatch
same name, same argument types
for normal methods, like C++ virtual functions
run-time type of object, static type of arguments
for static methods, based on static/declared type of
reference
use super to get at methods/variables in superclass
Overloading
same name, different argument types
CMCS 433, Fall 2001 -Alan Sussman 5
Constructors
Declaration syntax same as C++
no return type specified
method name same as class
First statement can/should be this(args)or
super(args)
if those are omitted, super() is called
must be very first statement, even before variable
declarations
not used for type conversions or assignments
void constructor generated if no constructors given
CMCS 433, Fall 2001 -Alan Sussman 6
Static class components
They belong to the class
static variables allocated once, no matter how
many objects created
static methods are not specific to any class
instance, so cant refer to this or super
Can reference class variables and methods
through either class name or an object ref
poor style to reference via object references
pf3

Partial preview of the text

Download Lecture Slides on C++project and Java Project | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433, Alan Sussman, U. Maryland

CMSC433, Spring 2001

Programming Language

Technology and Paradigms

Basic Java, continued

Alan Sussman

October 2, 2001

CMCS 433, Fall 2001 - Alan Sussman 2

Administrivia

  • C++ project
    • private drivers posted soon, grades by end of week
    • comments on other students projects due 10/
      • submit as project 11 (e.g. submit 11 1.txt 2.txt )
    • you will get the comments back for your project
  • Java project
    • default compiler is still 1.2, use the one in /usr/opt/java130/bin for 1.
    • try using jikes for compiling, in ~pugh/bin
      • faster than javac

CMCS 433, Fall 2001 - Alan Sussman 3

Administrivia (cont.)

  • Exam next Thursday, Oct. 11
    • practice exam on web page by tomorrow, talk about it on Tuesday
  • Guest lecturer on Thursday – Bill Pugh
    • he can answer any Java question, and probably most questions about project, but see TA or Dr. Porter for detailed project questions on Thursday or Friday
  • A warning
    • will be checking projects against each other for similarities (with a program) - and against similar projects from previous semesters CMCS 433, Fall 2001 - Alan Sussman 4

Last time - Java

  • Overriding methods – dynamic dispatch
    • same name, same argument types
    • for normal methods, like C++ virtual functions
      • run-time type of object, static type of arguments
    • for static methods, based on static/declared type of reference
    • use super to get at methods/variables in superclass
  • Overloading
    • same name, different argument types

CMCS 433, Fall 2001 - Alan Sussman 5

Constructors

  • Declaration syntax same as C++
    • no return type specified
    • method name same as class
  • First statement can/should be this(args) or super(args ) - if those are omitted, super() is called - must be very first statement, even before variable declarations
  • not used for type conversions or assignments
  • void constructor generated if no constructors given CMCS 433, Fall 2001 - Alan Sussman 6

Static class components

  • They belong to the class
    • static variables allocated once, no matter how many objects created
    • static methods are not specific to any class instance, so can’t refer to this or super
  • Can reference class variables and methods

through either class name or an object ref

  • poor style to reference via object references

CMSC 433, Alan Sussman, U. Maryland

CMCS 433, Fall 2001 - Alan Sussman 7

Interfaces

  • An interface is an object type – no

associated code or instance variables

  • only describes methods supported by interface
  • A class can implement (be a subtype of)

many interfaces

  • Interfaces may have final static variables
    • to define a set of constants (like enum in C++)

CMCS 433, Fall 2001 - Alan Sussman 8

Interface example

public interface Comparable { public int compareTo(Object o) } public class Util { public static void sort(Comparable []) { … } } public class Choices implements Comparable { public int compareTo(Object o) { return … ; } } … Choices [] options = … ; Util.sort(options); …

CMCS 433, Fall 2001 - Alan Sussman 9

No multiple inheritance

  • A class type can be a subtype of many other

types ( implements )

  • But can only inherit method

implementations from one superclass

( extends )

  • Not a big deal
    • multiple inheritance rarely, if ever, necessary and often poorly used
  • And it’s complicated to implement well CMCS 433, Fall 2001 - Alan Sussman 10

Garbage collection

  • Objects that are no longer accessible can be garbage collected
  • Method void finalize() called when an object is collected - best to avoid using it, since no way to tell when it will get called
  • Garbage collection not a major performance bottleneck - new/delete in C++ can be expensive too

CMCS 433, Fall 2001 - Alan Sussman 11

Class Objects

  • For each class, there is an object of type Class
  • Describes the class as a whole
    • used extensively in Reflection package
  • Class.forName(“MyClass”)
    • returns class object for MyClass
    • will load MyClass if needed
  • Class.forName(“MyClass”).newInstance()
    • creates a new instance of MyClass
  • MyClass.class gives the Class object for MyClass CMCS 433, Fall 2001 - Alan Sussman 12

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