August 28, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The Essence of OOP using Java, Array Objects, Part 2

  • June 5, 2002
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Assignment-compatible assignments

Values of the types byte, short, and int, are stored in the elements of the array object.

Actual type is lost in the process
(It should be noted that the byte and short values are converted to type int as they are stored.  When retrieved later, they will be retrieved as type int.  Any indication that these values were ever of any type other than int is lost in the process of storing and retrieving the values.)
What about class types?

If the declared element type is the name of a class, (which may or may not be abstract), a null reference or a reference to any object instantiated from the class or any subclass of the class may be stored in the array element.

(Obviously you can't store a reference to an object instantiated from an abstract class, because you can't instantiate an abstract class.)
What about an interface type?

If the declared element type is an interface type, a null reference or a reference to any object instantiated from any class that implements the interface can be stored in the array element.

(This is an extremely powerful concept, allowing references to objects instantiated from many different classes to be collected into an array as the interface type.)
Array reference variables

All array objects are accessed via references.  A reference variable whose declared type is an array type does not contain an array.  Rather, it contains either null, or a reference to an array object.

Allocation of memory

Declaring the reference variable does not create an array, nor does it allocate any space for the array components.  It simply causes memory to be allocated for the reference variable itself, which may later contain a reference to an array object.

Initialization

In the same sense that it is possible to declare a reference variable for an ordinary object, and initialize it with a reference to an object when it is declared, it is also possible to declare a reference to an array object and initialize it with a reference to an array object when it is declared.  This is illustrated in Listing 5, which shows the following operations combined into a single statement:

  • Declaration of a variable to contain a reference to an array object
  • Creation of the array object
  • Storage of the array object's reference in the reference variable
    int[] v1 = new int[3];

Listing 5

Can refer to different array objects

The length of an array is not established when the reference variable is declared.  As with references to ordinary objects, a reference to an array object can refer to different array objects at different points in the execution of a program.

For example, a reference variable that is capable of referring to an array of type int[] can refer to an array object of a given size at one point in the program and can refer to a different array object of the same type but a different size later in the program.

Placement of square brackets

When declaring an array reference variable, the square brackets [] may appear as part of the type, or following the variable name, or both.  This is illustrated in Listing 6.
 

    int[][] v1;
    int[] v2[];
    int v3[][];


Listing 6

Type and length

Once an array object is created, its type and length never changes.  A reference to a different array object must be assigned to the reference variable to cause the reference variable to refer to an array of different length.

Creating the actual array object

An array object is created by an array creation expression or an array initializer.

An array creation expression (or an array initializer) specifies:

  • The element type
  • The number of levels of nested arrays
  • The length of the array for at least one of the levels of nesting
Two valid array creation expressions are illustrated by the boldface statements in Listing 7.
 
    int[][] v1;
    int[] v2[];

    v1 = new int[2][3];
    v2 = new int[10][];

Listing 7

A two-dimensional rectangular array

The first boldface statement creates an array object of element type int with two levels of nesting.  This array object can be thought of as a traditional two-dimensional rectangular array having two rows and three columns.

A ragged array

The second boldface statement also creates an array object of element type int with two levels of nesting.  However, the number of elements in each row is not specified at this point, and it is not appropriate to think of this as a two-dimensional rectangular array.  In fact, once the number of elements in each row has been specified, it may not describe a rectangle at all.  Some authors refer to an array of this type as a ragged array.

The length of the array

The length of the array is always available as a final instance variable named length.  I will show you how to use the value of length in a sample program later in this lesson.

Accessing array elements

An array element is accessed by an array access expression.  The access expression consists of an expression whose value is an array reference followed by an indexing expression enclosed by matching square brackets.

The boldface expression in Listing 8 illustrates an array access expression (or perhaps two concatenated array access expressions).
 

    int[][] v1 = new int[2][3];
    System.out.println(v1[0][1]);

Listing 8




Page 2 of 8



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel