Published on

CS 3500 Day 5

  • avatar
    Jacob Aronoff

Object Oriented Design

As you may notice there is no part 4 because I didn't go to class that day. We jump into class today with basic Java arrays.


int[] array1 = new int[] {2, 4, 6, 8};
int[] array2 = new int[54];
String[] array3 = new String[23];

Here's how we use them:

int[] intArray = {2, 4, 6, 8};
assertEquals(4, intArray[1]);
assertEquals(8, intArray[3]);

intArray[3] = 17;

assertEquals(17, intArray[3]);

assertEquals(4, intArray.length);

Aliasing is very important with arrays!!!

int[] a1 = new int[16];
int[] a2 = new int[16];
int[] a3 = a1;
aE( 0, a1[7] );
aE( 0, a2[7] );
aE( 0, a3[7] );
a1[7] = 1;
aE( 1, a1[7] );
aE( 0, a2[7] );
aE( 1, a3[7] );
a2[7] = 2;
aE( 1, a1[7] );
aE( 2, a2[7] );
aE( 1, a3[7] );
a3[7] = 3;
aE( 3, a1[7] );
aE( 2, a2[7] );
aE( 3, a3[7] );


Aliasing is not copying!!! In order to copy an array we need to deep copy the array as so:

int[] a1 = new int[16];
int[] a2 = new int[16];
//create copy of a1
int[] a3 = new int[a1.length];
for (int i = 0; i < a1.length; i++) {
   a3[i] = a1[i];

// alternative way of copying
   a3 = Arrays.copyOf(a1,a1.length);

a1[7] = 1;
aE( 1, a1[7] );
aE( 0, a2[7] );

Now we're getting in to variable arguments which is really cool in Java:

public void printInts(int[] intArray) {
    for (int i : intArray) {
printInts(new int[] { 8, 6, 7 });
// now with varargs
public void printInts(int... intArray) {
    for (int i : intArray) {
printInts(8, 6, 7);

When should we use them?

  • An existing API requires it
  • To ensure sequence length is fixed
  • Efficiency, especially when implementing highger level data structures

Arrays are indexed starting at 0 because when you multiply 0 by the size you always get the first element. Arrays can be indexed from 0 to it's length - 1.

Arrays vs Linked list

Arrays can't change in size, and you cant really insert and remove from arrays. Linkedlist you can remove things, add things, etc. Array's of bigger sizes reserve a ton of memory which may not be good.


A useful diagram for visualizing what references may look like:


Now we're getting into how the JVM and the java compiler works. It's unneccesary to talk about here so im not going to.

Primative vs Boxed:

for each primitive, there is a boxed:

Primitive Boxed boolean Boolean byte Byte short Short char Character int Integer float Float long Long double Double

Now onto Null


Null is used for when reference types are not initialized.

Rules of null

  • Use it only to represent the absence of a reference
  • Use it conscientiously and sparingly!
  • Carefully document where it's allowed
  • Check for it and fail fast


Types of equality

  • shallow vs. deep (same memory location, or same memory contents but at different locations)
  • intensional vs. extensional (literally the same thing, or calculates/derives to the same thing)
  • nominal vs. structural (are objects of two classes with same instance variables and values equal?)
  • physical vs. logical (Are HMSDuration and CompactDuration objects with same duration equal?)
  • == vs. equals()?


  • The Rules of equals():
    • Reflexivity: x.equals(x)
    • Symmetry: x.equals(y) iff y.equals(x)
    • Transitivity: if x.equals(y) and y.equals(z) then x.equals(z)
  • The Rule(s) of hashCode():
    • Compatibility: if x.equals(y) then x.hashCode() == y.hashCode()
    • Non-injectivity: x.hashCode() == y.hashCode() doesn't contradict !x.equals(y)
  • Rule 9 (corollary): "Always override hashCode when you override equals" ---Joshua Bloch, Effective Java 2/E

Next class is exceptions, and more basic/ interesting java principles.