<< Chapter < Page Chapter >> Page >

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 leavethat 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. Hereis 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.

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."

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.

Class objects representing array objects

An object of the class named Class can be obtained (by calling 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 module.)

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 Cprogrammer, 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 .

StringBuffer objects

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.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask