<< Chapter < Page Chapter >> Page >
Listing 8 . An array access expression.
int[][]v1 = new int[2][3]; System.out.println(v1[0][1]);

First-level access

This array access expression first accesses the contents of the element at index 0 in the array object referred to by the reference variablenamed v1 . This element contains a reference to a second array object (note the double matching square brackets, [][]in the declaration of the variable named v1 ).

Second-level access

The array access expression in Listing 8 uses that reference to access the value stored in the element at index value 1 in thesecond array object. That value is then passed to the println method for display on the standard output device.

(In this case, the value 0 is displayed, because array elements are automatically initialized to default values when the array object is created.The default value for all primitive numeric values is zero.)

Zero-based indexing

All array indexes in Java begin with 0 . An array with length n can be indexed by the integers 0 to (n-1) . Array accesses are checked at runtime. If an attempt is made to access the array with any otherindex value, an ArrayIndexOutOfBoundsException will be thrown.

Index value types

Arrays must be indexed by integer values of the following types: int , short , byte , or char . For any of these types other than int , the value will be promoted to an int and used as the index.

An array cannot be accessed using an index of type long . Attempting to do so results in a compiler error.

Default initialization

If the elements in an array are not purposely initialized when the array is created, the array elements will be automatically initialized with defaultvalues. The default values are:

  • All reference types: null
  • Primitive numeric types: 0
  • Primitive boolean type: false
  • Primitive char type: the Unicode character with 16 zero-valued bits

Explicit initialization of array elements

The values in the array elements may be purposely initialized when the array object is created using a comma-separated list of expressions enclosed bymatching curly brackets. This is illustrated in Listing 9 .

Listing 9 . Explicit initialization of array elements.
int[] v1 = {1,2,3,4,5};

No new operator

Note that this format does not use the new operator. Also note that the expressions in the list may be much more complex than the simpleliteral values shown in Listing 9 .

Length and order

When this format is used, the length of the constructed array will equal the number of expressions in the list.

The expressions in an array initializer are executed from left to right in the order that they occur in the source code. The first expression specifies thevalue at index value zero, and the last expression specifies the value at index value n-1 (where n is the length of the array).

Each expression must be assignment-compatible with the array's component type, or a compiler error will occur.

A sample program

The previous paragraphs in this module have explained some of the rules and characteristics regarding array objects. They have also illustrated some of thesyntax involved in the use of array objects in Java.

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