Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 3 ■ DATA TYPES

lang/Boolean.html and the JavaDoc for the Character class at http://download.oracle.com/javase/7/
docs/api/java/lang/Character.html. You can find all the JavaDoc for Java 7 at http://download.oracle.
com/javase/7/docs/api/ (as you can see, the other topics are branches of the overall API
documentation).


Arrays


An array is a data structure that holds a group of variables under a single identifier. Java supports arrays
for both primitives and objects. Square brackets after a variable's name indicate that it is an array.
Listing 3-7 shows several arrays and how to manipulate them.


Listing 3-7. Arrays of primitives


int[] a; // array declaration without assignment
a = new int[2]; // specify the length of an existing array
int[] b = {1, 2, 3, 4}; // array declaration with assignment
int bLength = b.length; // how to get the length of an array


// arrays start at 0, not 1
b[0] = 2; // have to reassign each value in the array individually


Let's look at the code one line at a time. The first line creates an array of int primitives but doesn't
assign anything to it (a is null at that point). The second line shows how to set an array to be a particular
length. If a had values, its values would be replaced by the default values of the new type (0 in this case).
The third line shows how to create an array with a set of starting values. You can use that block
assignment syntax only when creating an array, not when assigning new values to an existing array. The
fourth line shows how to get the length of an array. The last line shows how to reassign one of the values
in an array and shows that array addresses start at 0. (Most programming languages start counting at 0.)
Now let's consider an array of objects in Listing 3-8, which reveals some interesting things.


Listing 3-8. Arrays of objects


Integer[] myIntegers = new Integer[4];
for (int i = 0; i < myIntegers.length; i++) {
myIntegers[i] = new Integer(i);
}


Again, let's go line by line. As you can see, the syntax for creating an array of objects differs a bit
from that for primitives. The part to the left of the equal sign looks the same (the kind of object, the array
indicator, and the name of the variable), but the part to the right of the equal sign differs by having the
new keyword before the type of the item going into the array. To create a new instance of an object, we
usually use the new keyword (though other ways exist), which calls a constructor for the object. However,
all of the Integer class's constructors require an object (either an int or a String object that holds an
integer value), so this array ends up holding four null references. (We dive into what null means in a
moment.)
This listing also shows how to loop through an array and, in this case, create an object for each of
the null objects. Notice that we have to use the new keyword again, even though we used it when we
created the array. Because we got four null references rather than actual objects from the original
assignment, we have to create new ones here. In this case, we end up with four Integer objects having
values of 0, 1, 2, and 3.

Free download pdf