gamelan
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See the Winners!


Linked Data Planet Conference & Expo


Developer Jobs

Be a Commerce Partner
KVM over IP
Corporate Awards
Phone Cards
Promotional Gifts
Baby Photo Contest
Condos For Sale
Remote Online Backup
Career Education
Logo Design Custom
Desktop Computers
Computer Hardware
Find Software
KVM Switches
Compare Prices

 


  Managing the Modern Network
Sponsored by HP
In a global economy where information crosses the globe in an instant, and where Web-based applications power business, it's more important than ever to ensure your network is safe from threats and optimized to deliver the data your business needs. »
 
  Business Service Management: Generate Revenue Through IT
Sponsored by HP
IT must now help organizations attract, retain and grow customer relationships and increase customer satisfaction. Business service management (BSM) helps lay the foundation by managing services in dynamic support of business requirements. Learn more. »
 
  Evaluating Software as a Service for Your Business
Sponsored by Webroot
Is Software as a Service just hype, or is something really going on here? See if your company can benefit as SaaS tries to change the face of the enterprise. »
 
  Storage Networking: Configuration and Planning
Sponsored by HP
The most critical part of setting up a SAN is configuring each individual disk array. This guide examines configurations for SAN-attached servers and disk arrays, and looks at the future of IP storage. »
 
  Is Your Disaster Recovery Plan Good Enough?
Sponsored by HP
Preparing for a disaster is more often than not part of the storage planning process, and it is one of the most difficult tasks, since it includes local hardware and software, networking equipment, and a test plan. Learn how to get disaster recovery right. »
 
Developer News -
SaaS Tool Offers Custom Database Development    May 9, 2008
Microsoft’s Automated Agent: Can We Talk?    May 7, 2008
Borland Finally Sells CodeGear    May 7, 2008
Red Hat Heads For The JON 2.0    May 7, 2008
Free Tech Newsletter -

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

The Essence of OOP using Java, Array Objects, Part 3
By Richard G. Baldwin

Go to page: 1  2  3  4  Next  

... in Java by Richard G Baldwin

Java Programming Notes # 1626


Preface

This series of lessons is designed to teach you about the essence of Object-Oriented Programming (OOP) using Java.

The first lesson in the group was entitled The Essence of OOP Using Java, Objects, and Encapsulation.  The previous lesson was entitled The Essence of OOP using Java,  Array Objects, Part 2.

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.

For further reading, see my extensive collection of online Java tutorials at Gamelan.com. A consolidated index is available at Baldwin's Java Programming Tutorials.

Preview

This lesson discusses various details regarding the use of array objects in Java, including:
  • The members of an array object
  • The interfaces implemented by array objects
  • Class objects and array objects
  • The classes named Array and Arrays

Discussion and Sample Code

Members of an array object

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).
Implements Cloneable and Serializable

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.

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

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.

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

Go to page: 1  2  3  4  Next  

Previous article: The Essence of OOP using Java, Array Objects, Part 2
Next article: The Essence of OOP using Java, The this and super Keywords


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Other Java Archives

Guide to Developing a Web Site. Best Practices, Tips and Strategies. Download Exclusive eBook Now.
Intel Go Parallel Portal: Translating Multicore Power into Application Performance
Is it time to make your move to the multi-threaded and parallel processing world? Find out!
Data Sheet: IBM Information Server Blade
Developing Intelligent Communications? Visit the Avaya DevConnect Center on DevX.



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES