Thursday, 4 December 2008

Java Collections By John Zukowski - Part1

Today i started reading Java Collections by John Zukowski.

Nice book written by John Zukowski.

Few important quotations i wanted to share from the first 2 chapters.

1) Arrays are the only collection support defined within the Java programming language. They are objects that store a set of elements in an order accessible by index, or position. They are a subclass of Object and implement both the Serializable and Cloneable interfaces. However, there is no .java source file for you to see how the internals work. Basically, you create an array with a specific size and type of element, then fill it up.

2) Since arrays subclass Object, you can synchronize on an array variable and call its wait() and notify() methods.

3) Array Basics

Before going into the details of declaring, creating, initializing, and copying arrays, let's go over a simple array example. When creating a Java application, the main() method has a single argument that is a String array: public static void main(String args[]). The compiler doesn't care what argument name you use, only that it is an array of String objects.

Given that we now have the command−line arguments to our application as an array of String objects, we can look at each element and print it. Within Java, arrays know their size, and they are always indexed from position zero. Therefore, we can ask the array how big it is by looking at the sole instance variable for an array: length. The following code shows how to do this:


public class ArrayArgs {
public static void main (String args[]) {
for (int i=0, n=args.length; i<n; i++) {
System.out.println("Arg " + i + ": " + args[i]);
}
}
}



4) When copying elements between different arrays, if the source or destination arguments are not arrays or their types are not compatible, an ArrayStoreException will be thrown. Incompatible arrays would be where one is an array of primitives and the other is an array of objects; or the primitive types are different; or the object types are not assignable.

5) When copying arrays with arraycopy(), the source and destination arrays can be the same if you want to copy a subset of the array to another area within that array. This works even if there is some overlap.Since arrays implement the Cloneable interface, besides copying regions of arrays, you can also clone them.

Cloning involves creating a new array of the same size and type and copying all the old elements into the new array. This is unlike copying, which requires you to create and size the destination array yourself. In the case of primitive elements, the new array has copies of the old elements, so changes to the elements of one are not reflected in the copy. However, in the case of object references, only the reference is copied. Thus, both copies of the array would point to the same object. Changes to that object would be reflected in both arrays. This is called a shallow copy or shallow clone.

6)Array Immutability

It is useful to return an array clone from a method if you don't want the caller of your method to modify the underlying array structure. While you can declare arrays to be final, as in the following example:


final static int array[] = {1, 2, 3, 4, 5};


declaring an object reference final (specifically, an array reference here) does not restrict you from modifying the object. It only limits you from changing what the final variable refers to. While the following line results in a compilation error:


array = new int[] {6, 7, 8, 9};


changing an individual element is perfectly legal:


array[3] = 6;


7) Checking for Array Equality

Checking for equality between two arrays can be done in one of two manners depending upon the type of equality you are looking for. Are the array variables pointing to the same place in memory and thus pointing to the same array? Or are the elements of two arrays comparatively equivalent?

Checking for two references to the same memory space is done with the double equal sign operator ==. For example, the prior components and buttons variables would be equal in this case since one is a reference to the other:

components == buttons // true

However, if you compare an array to a cloned version of that array then these would not be equal as far as == goes. Since these arrays have the same elements but exist in different memory space, they are different. In order to have a clone of an array be "equal" to the original, you must use the equals() method of the java.util.Arrays class.

String[] clone = (String[]) strarray.clone();
boolean b1 = Arrays.equals(strarray, clone); // Yes, they're equal

About the Author

John Zukowski has been involved with the Java platform since it was just called Java, 11 years and running, since 1995. He is actively working with SavaJe Technologies to finish up the JavaOne 2006 device of show: the Jasper S20 mobile phone. He currently writes a monthly column for Sun’s Core Java Technologies Tech Tips and Technology Fundamentals Newsletter. He has contributed content to numerous other sites, including jGuru, DevX, Intel, and JavaWorld. He has written many other popular titles on Java, including Java AWT Reference (O’Reilly), Mastering Java 2 (Sybex), Borlands’ JBuilder: No Experience Required (Sybex), Learn Java with JBuilder 6 (Apress), Java Collections (Apress), and The Definitive Guide to Swing (Apress).

No comments: