<< Chapter < Page Chapter >> Page >
  • All arrays in Java are one-dimensional arrays. (Multidimensional arrays are created by creating tree structures of one-dimensional arrays.)
  • Each array in Java is encapsulated in a special type of object that I will refer to as an array object .
  • As with all objects, an array object must be accessed using a reference to the array object.
  • When the declared type of an array is one of the eight primitive types, the actual values are stored in the array elements in the array object.
  • When the declared type of an array is the type of an object (array object or ordinary object), references to the objects are stored in the array elements and the objects actually exist elsewhere in memory.
  • As with instance variables, the elements in an array are typically initialized with the standard default values for the types involved (zero, false, or null) . That is not the case in this program however.
  • The array that is encapsulated in an array object may have none, one, or more elements. (Yes, it is possible for a Java array to have no elements, but that normally occurs only in special circumstances.)
  • The length or size of the array is established when the array object is instantiated and cannot be changed thereafter.
  • Every array object contains a special property named length that contains the number of elements in an array. It is always possible todetermine the number of elements in an array object at runtime by accessing the value of the length property for the array object.

A special instantiation syntax

There is a special syntax that allows for the instantiation of an array object and the initialization of the array elements in a single statement. (I explain this here , here , and here so you should already know all about it.) The last statement in Listing 1 is an example of this syntax.

Briefly, the syntax consists of a comma separated list of element values (expressions) inside a pair of matching curly braces. The length of the array is determined by the number of values in the list. The type of thearray is determined by the types of the elements in the list.

This syntax instantiates an array object of the correct length and populates the elements with the specified values.

A reference is returned

A reference to the array object is returned in much the same way that a constructor for an ordinary object returns a reference to the object.

As is always the case, if the reference is stored in a variable, the type of the reference must be assignment compatible with the type of the variable.

What is assignment compatible?

If you have forgotten what this term means, I recommend that you go to Google and search for the following keywords to learn more about it:

site:http://cnx.org/contents/ "assignment compatible" java

A one-element array

The last statement in Listing 1 instantiates an array object containing a one-element array. The array element is initializedwith a reference to a new object of type Prob05MyClassA , which exists somewhere else in memory.

The value of a random number that was generated earlier in the main method is passed as a parameter to the constructor for the object of type Prob05MyClassA .

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