Summary: Debugging Interlude—Debugging Arrays

In this lesson, we will summarize some of the main sources of errors encountered when working with Java arrays.

  • Allocate an array soon after writing its declaration to avoid an easy-to-make mistake.

  • Never give a client of your class direct access to the class’s private data fields that are arrays or other objects. That is, do not define a public method that returns a reference to such a field. In this way, you ensure that a client cannot corrupt the behavior of your class.

  • Indices to array elements in Java have integer values that lie between zero and one less than the size of the array. Failure to observe this limitation will lead to an ArrayIndexOutOfBounds exception.

Get hands-on with 1200+ tech skills courses.