Lecture 22: Arrays in Java How does Java do arrays? + arrays are objects! + all elements must have the same type (or class) + indicies must be integers or expressions that evaluate to integers Steps for creating: + declare: type[] name type name[] + assign: name = new type[size] + shortcut version: type[] name = new type[size] (more "versions" show up later) Important notes/features: + [] is operator and has highest precedence + can declare arrays in same statement but be careful: ex) int a[], b; -> b is NOT an array + why $new$? arrays are objects + size is number of elements + labeling of indicies starts at zero! + if you attempt to access index that does not exist, Java complains + can find length automatically with name.length How to think of objects? + arrays "live" in $Object$ class + draw a box to represent the array + draw boxes to represent individual elements + elements get initial default values ("zeros") + each element resembles a reference variable (helps when dealing with arrays of objects)