Outline

Exam preparation

Java Review

public static int foo() {
  return true;
}

is incorrect

public static int foo(int n) throws java.io.IOException {
  if (n < 0)
    throw new java.io.IOException();
  else if (n == 0)
    throw new java.util.NoSuchElementException();
  else
    return n - 1;
}

Abstract Data Types

Interfaces

public interface Iterable<E> {
  java.util.Iterator<E> iterator();
}
LinkedList<String> list = new LinkedList<String>();
Iterable<String> iterable = list;
Iterator<String> iter = iterable.iterator();

Class Hierarchy, Abstract Classes, Inheritance

Lists

Array List

Linked List

Runtime analysis

Iterators

List<E> list = ...
Iterator<E> iter = list.iterator();
while (iter.hasNext()) {
  E variable = iter.next();
  // can use "variable" in the loop
}
for (E variable: Iterable<E>) {
  // can use "variable" in the loop
}

Java References

String a = new String("foo");
String b = new String("foo");
if (a == b) {
  System.out.println("two different objects are equal!  Something is wrong");
}
if (a.equals(b)) {
  System.out.println("foo is equal to foo.  Life is good");
}
int x = 3;
m(x);

The value of x is still 3 after the call, no matter what m does.

LinkedList<Integer> x = new LinkedList<Integer>();
...
m(x);
...
void m(LinkedList<Integer> parameter) {
  parameter = new LinkedList<Integer>();
}

x still refers to the original linked list (not the new list) after the call.

LinkedList<Integer> x = new LinkedList<Integer>();
...
m(x);
...
void m(LinkedList<Integer> parameter) {
  parameter.add(13);
}

after this call, x refers to the same list, which now has a new element.