Docsity

Java Programming: Equality, Exceptions, and Project Instructions (CMSC433, Spring 2004), Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC433, Spring 2004
Programming Language Technology and
Paradigms
Java Review
Jeff Foster
February 5, 2004
67
Administrivia
•Project submission instructions posted on-line
–Project due Wednesday
•Reading: Liskov ch 3, 9, 10
68
Project 1 Notes
•Project 1 shows some of the issues in software
components
–And a few design patterns as well (more later)
•MiniServlet is the interface between components
–WebServer shouldn’t assume anything about servlets it
will run
–MiniServlets shouldn’t assume anything about server
that will invoke them
69
Multiple Vendors
70
Things to Check
•If “grep Proxy WebServer.java” finds a match,
you likely have a problem
•Can you put just WebServer.java and
MiniServlet.java in a directory and compile them?
•Can you put everything except WebServer.java in
a directory and compile them all?
•Can you use TestServlet to run your servlets?
71
Java Classes and Objects
•Each object is an instance of a class
–An array is an object
•Each class extends one superclass
–Object if not specified
–Class Object has no superclass
pf3
pf4

Partial preview of the text

Download Java Programming: Equality, Exceptions, and Project Instructions (CMSC433, Spring 2004) and more Study notes Programming Languages in PDF only on Docsity!

CMSC433, Spring 2004

Programming Language Technology and

Paradigms

Java Review

Jeff Foster

February 5, 2004

67

Administrivia

• Project submission instructions posted on-line

  • Project due Wednesday

• Reading: Liskov ch 3, 9, 10

68

Project 1 Notes

• Project 1 shows some of the issues in software

components

  • And a few design patterns as well (more later)

• MiniServlet is the interface between components

  • WebServer shouldn’t assume anything about servlets it will run
  • MiniServlets shouldn’t assume anything about server that will invoke them 69

Multiple Vendors

70

Things to Check

• If “grep Proxy WebServer.java” finds a match,

you likely have a problem

• Can you put just WebServer.java and

MiniServlet.java in a directory and compile them?

• Can you put everything except WebServer.java in

a directory and compile them all?

• Can you use TestServlet to run your servlets?

71

Java Classes and Objects

• Each object is an instance of a class

  • An array is an object

• Each class extends one superclass

  • Object if not specified
  • Class Object has no superclass

72

Objects Have Methods

• All objects, therefore, inherit them

  • Default implementations may not be the ones you want public boolean equals (Object that) – “conceptual” equality public String toString() – returns printable representation public int hashCode() – key for hash table public void finalize() – called if object is garbage collected
  • And others … 73

Equality

• Object .equals(Object) method

  • Structural (“conceptual”) equality

• == operator (!= as well)

  • True if arguments reference the same object
  • o == p implies o.equals(p) 74 class Foo { public boolean equals(Foo f) { … } // wrong! } class Foo { public boolean equals(Object o) { // right! if (!(o instanceof Foo)) return false; ... } } The first case creates an overloaded method, while the second overrides the parent (Object) method.

Overriding Equals

75

Overriding hashCode

• hashCode() is used for objects that may be stored

in hash table

• Rule of thumb: If you override equals() or

hashCode(), you should also override the other

  • a.equals(b) implies a.hashCode() == b.hashCode() 76

Preconditions

• Functions often have requirements on their inputs

// Return maximum element in A[i..j] int findMax(int[] A, int i, int j) { ... }

  • A is non-empty
  • i and j must be non-negative
  • i and j must be less than A.length
  • i < j (maybe)

• These are called preconditions or requires clauses

77

Dealing with Errors

• What do you do if a precondition isn’t met?

• What do you do if something unexpected

happens?

  • Try to open a file that doesn’t exist
  • Try to write to a full disk

84

Exception Handling

• All exceptions eventually get caught

• First catch with supertype of the exception

catches it

• finally is always executed

try { if (i == 0) return; myMethod(a[i]); } catch (ArrayIndexOutOfBounds e) { System.out.println(“a[] out of bounds”); } catch (MyOwnException e) { System.out.println(“Caught my error”); } catch (Exception e) { System.out.println(“Caught” + e.toString()); throw e; } finally { /* stuff to do regardless of whether an exception / / was thrown or a return taken */ } 85

Masking Exceptions

• Handle exception and continue

while ((s = ...) != null) { try { FileInputStream f = new FileInputStream(s); ... } catch (FileNotFoundException e) { System.out.println(s + “ not found”); } } 86

Reflecting Exceptions

• Pass exception up to higher level

  • Automatic support for throwing same exception
  • Sometimes useful to throw different exception try { ... a[5] ... } catch (IndexOutOfBoundsException e) { throw new EmptyException(“Arrays.min”); } 87

Exception Chaining

• Indicate the cause of a thrown exception

  • Specify the exception that caused this one
  • Shows cause chain in stack trace try { ... a[0] ... } catch (IndexOutOfBoundsException e) { // e can be retrieved from exn with getCause() throw new Exception(“Arrays.min”, e); } 88

Exception Hierarchy

Throwable Error Exception RuntimeException Checked Unchecked 89

Unchecked Exceptions

• Subclasses of RuntimeException and Error are

unchecked

  • Need not be listed in method specifications

• Currently used for things like

  • NullPointerException
  • IndexOutOfBoundsException
  • VirtualMachineError

• Is this a good design?