Arrays

You can think of a Java array as a special type of object that knows the data type it contains and the number of these items it can hold. Arrays can hold primitives, references to regular objects, and references to other arrays. Be sure to remember that the addressing of elements in an array starts with zero. One of the major safety features of Java is that every attempt to address an array element is checked to ensure that the bounds are not exceeded.

Declaring an Array

Array variables may be declared separately from any initialization of the array elements. Here are some examples of array declarations:

1.  int[] counts ;
2.  String names[] ;
3.  boolean flags[][] ;
4.  boolean[] flags[] ;

Statement 1 declares that the variable counts

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.