6.13. Chapter Summary

 
[Page 201 ( continued )]

Chapter Summary

  • A variable is declared as an array type using the syntax dataType[] arrayRefVar or dataType arrayRefVar[] . The style dataType[] arrayRefVar is preferred, although dataType arrayRefVar[] is legal.

  • Unlike declarations for primitive data type variables , the declaration of an array variable does not allocate any space in memory for the array. An array variable is not a primitive data type variable. An array variable contains a reference to an array.

  • You cannot assign elements to an array unless it has already been created. You can create an array by using the new operator with the following syntax: new dataType[arraySize] .

  • Each element in the array is represented using the syntax arrayRefVar[index] . An index must be an integer or an integer expression.

  • After an array is created, its size becomes permanent and can be obtained using arrayRefVar.length . Since the index of an array always begins with , the last index is always arrayRefVar.length - 1 . An out-of-bounds error will occur if you attempt to reference elements beyond the bounds of an array.

  • Programmers often mistakenly reference the first element in an array with index 1 , so that the index of the tenth element becomes 10 . This is called the index off-by-one error .

  • Java has a shorthand notation, known as the array initializer , which combines declaring an array, creating an array, and initializing in one statement using the syntax:

       dataType[] arrayRefVar = {value0, value1, ..., value  k  }   . 

  • When you pass an array argument to a method, you are actually passing the reference of the array; that is, the called method can modify the elements in the caller's original array.

  • You can use arrays of arrays to form multidimensional arrays. For example, a two-dimensional array is declared as an array of arrays using the syntax dataType[][] arrayRefVar or dataType arrayRefVar[][] .

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net