<< Chapter < Page Chapter >> Page >
Listing 3 . The generic class Object.
Object[] v3;

Primitive type conversions

Similarly, if the declared element type for the array object is one of the primitive types, the elements of the array can be used to store values of anyprimitive type that is assignment compatible with the declared type (without the requirement for a cast).

For example, the code in Listing 4 shows the creation of a one-dimensional array object capable of storing values of type int . The array object has a length of 3 elements, and the object's reference is stored in a referencevariable named v1 .

Listing 4 . Primitive type conversions.
int[] v1;v1 = new int[3];byte x1 = 127; short x2 = 16384;int x3 = 32000; v1[0]= x1; v1[1]= x2; v1[2]= x3;

Assignment-compatible assignments

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

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 theinterface 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 theinterface 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 beallocated 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 andinitialize 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

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