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.

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

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) {
        System.out.println(i);
    }
}
printInts(new int[] { 8, 6, 7 });
// now with varargs
public void printInts(int... intArray) {
    for (int i : intArray) {
        System.out.println(i);
    }
}
printInts(8, 6, 7);

When should we use them?

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.

References

A useful diagram for visualizing what references may look like:

references

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

Null is used for when reference types are not initialized.

Rules of null

Equality

Types of equality

RULES

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