Equality, Overriding Equals and HashCode, Preconditions, Errors and Exceptions in Java, Study notes of Programming Languages

Various aspects of handling equality, overriding equals and hashcode, preconditions, dealing with errors, signaling errors, and using exceptions in java. It covers the concept of structural equality, the use of the equals() method, the importance of overriding hashcode(), preconditions, error handling, signaling errors with different styles, problems with these approaches, and the use of exceptions as a form of non-local control-flow.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-qiw
koofers-user-qiw 🇺🇸

9 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
68
Equality
Object .equals(Object) method
Structural (“conceptual”) equality
== operator (!= as well)
True if arguments reference the same object
o == p implies o.equals(p)
69
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
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Equality, Overriding Equals and HashCode, Preconditions, Errors and Exceptions in Java and more Study notes Programming Languages in PDF only on Docsity!

68

Equality

• Object .equals(Object) method

  • Structural (“conceptual”) equality

• == operator (!= as well)

  • True if arguments reference the same object
  • o == p implies o.equals(p) 69 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

70

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() 71

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

74

Signaling Errors (cont’d)

  • Style 2: Return an invalid value and status *static int lock_rdev(mdk_rdev_t rdev) { ... if (bdev == NULL) return -ENOMEM; ... } // Returns NULL if error and sets global // variable errno **FILE *fopen(const char path, const char mode); 75

Problems with These Approaches

• What if all possible return values are valid?

  • E.g., findMax from earlier slide

• What if client forgets to check for error?

  • No compiler support

• What if client can’t handle error?

  • Needs to be dealt with at a higher level

76

Exceptions in Java

• On an error condition, we throw an exception

• At some point up the call chain, the exception is

caught and the error is handled

• Separates normal from error-handling code

• A form of non-local control-flow

  • Like goto, but structured 77

Throwing an Exception

• Create a new object of the class Exception , and

throw it

if (i >= 0 && i < a.length ) return a[i]; else throw new ArrayIndexOutOfBounds();

• Exceptions thrown are part of return type

  • When overriding method in superclass, cannot throw

any more exceptions than parent’s version

80

Masking Exceptions

• Handle exception and continue

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

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”); }

82

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); } 83

Exception Hierarchy

Throwable

Error Exception

RuntimeException

Checked

Unchecked