The Essence of OOP using Java, Array Objects, Part 3
Java Programming Notes # 1626
You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different listings while you are reading about them.
- The members of an array object
- The interfaces implemented by array objects
- Class objects and array objects
- The classes named Array and Arrays
An array object has the following members (in addition to the data stored in the object):
- A public final variable named length, which contains the number of components of the array (length may be positive or zero)
- A public method named clone. This method overrides the method of the same name in Object class.
- Default versions of all the methods inherited from the class named Object, (other than clone, which is overridden as described above).
Also, every array object implements the Cloneable and Serializable interfaces. (Note that neither of these interfaces declares any methods.)
What is the Cloneable interface?
Here is what Sun has to say about the Cloneable interface:
"A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Attempts to clone instances that do not implement the Cloneable interface result in the exception CloneNotSupportedException being thrown."Thus, the fact than an array object implements the Cloneable interface makes it possible to clone array objects.
A cloned array is shallow
While it is possible to clone arrays, care must be exercised when cloning multidimensional arrays. That is because a clone of a multidimensional array is shallow.
What does shallow mean?
Shallow means that the cloning process creates only a single new array.
Subarrays are shared between the original array and the clone.
(Although I'm not certain, I suspect that this may also be the case for cloning array objects containing references to ordinary objects. I will leave that determination as an exercise for the student. In any event, be careful if you clone array objects.)Serialization
Serialization of an object is the process of decomposing the object into a stream of bytes, which can later be recomposed into a copy of the object. Here is what Sun has to say about the Serializable interface:
"Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized.Even though this quotation from Sun doesn't address array objects, because array objects implement the Serializable interface, they can be serialized and later reconstructed.
All subtypes of a serializable class are themselves serializable.
The serialization interface has no methods or fields and serves only to identify the semantics of being serializable."
Class objects representing array objects
An object of the class named Class can be obtained (by invoking the getClass method of the Object class) to represent the class from which an ordinary object was instantiated.
The Class object is able to answer certain questions about the class that it represents (such as the name of the superclass), and has other uses as well.
(One of the other uses is to specify the type as a parameter to the methods of the Array class, which I will illustrate later in this lesson.)Every array also has an associated Class object.
That Class object is shared with all other arrays with the same component type.
The superclass of an array type is Object. (Think about this!)
An array of characters is not a string
For the benefit of the C/C++ programmers in the audience, an array of char is not a String. (In Java, a string is an object of the String class or the StringBuffer class).
Not terminated by null
Also, neither a String object nor an array of type char is terminated by '\u0000' (the NUL character). (This information is provided for the benefit of C programmers who are accustomed to working with so-called null-terminated strings. If you're not a C programmer, don't worry about this.)
A String object in Java is immutable
Once initialized, the contents of a Java String object never change.
On the other hand, an array of type char has mutable elements. The String class provides a method named toCharArray, which returns an array of characters containing the same character sequence as a String.
The class named StringBuffer also provides a variety of methods that work with arrays of characters. The contents of a StringBuffer object are mutable.
The Array and Arrays classes
The classes named Array and Arrays provide methods that you can use to work with array objects.
The Array class provides static methods to dynamically create and access Java arrays.
The Arrays class contains various methods for manipulating arrays (such as sorting and searching). It also contains a static factory method that allows arrays to be viewed as lists.
A sample program named Array08
The sample program named Array08 (shown in Listing 12 near the end of the lesson) illustrates the use of some of these methods.
Will discuss in fragments
As usual, I will discuss this program in fragments. Essentially all of the interesting code is in the method named main, so I will begin my discussion there. The first few fragments will illustrate the creation, population, and display of a one-dimensional array object whose elements contain references to objects of type String.
The newInstance method of the Array class
The code in Listing 1 invokes the static method of the Array class named newInstance to create the array object and to store the object's reference in a reference variable of type Object named v1.
(Note that there are two overloaded versions of the newInstance method in the Array class. I will discuss the other one later.)
Object v1 = Array.newInstance( Class.forName( "java.lang.String"), 3); Listing 1
Page 1 of 4