Java Reference Index

  1. Introduction
  2. Data Types
  3. Arrays
  4. Operators
  5. Program Control
  6. Classes
  7. Objects
  8. Interfaces
  9. Containers
  10. Class Structure
  11. Error Handling

    Program Types

  12. Applications
  13. Applets

    How To

  14. Type Conversions


  15. java.lang.reflect
  16. java.applet
  17. java.awt
  18. java.beans
  20. java.lang
  21. java.math
  23. java.rm
  26. java.text
  27. java.util
  28. java.servlet


  29. Object Class


  30. Terms
  31. Credits


The first step in array creation is to declare a variable that will hold the array. Array variables indicate the object or data type the array will hold and the name of the array. To differentiate from regular variable declarations, a pair of empty brackets ([]) is added to the object or data type, or to the variable name. The following statements are examples of array variable declarations:

String difficultWords[];
Point hits[];
int donations[];

Because you can put the brackets after the information type instead of the variable name, the following statements also work:

String[] difficultWords;
Point[] hits;
int[] guesses;

You see both styles used in programs, and because there isn’t a consensus on which style is more readable, the choice is largely a matter of personal preference. "

" After you declare the array variable, the next step is to create an array object and assign it to that variable. The ways to do this step are as follows:

  • Use the new operator.
  • Initialize the contents of the array directly.

Because arrays are objects in Java, you can use the new operator to create a new instance of an array, as in the following statement:

String[] playerNames = new String[10];

This statement creates a new array of strings with 10 slots that can contain String objects. When you create an array object using new, you must indicate how many slots the array will hold. This statements does not put actual String objects in the slots—you must do that later. Array objects can contain primitive types such as integers or Booleans, just as they can contain objects:

int[] temps = new int[99];

When you create an array object using new, all of its slots automatically are initialized (0 for numeric arrays, false for Boolean, ‘\0’ for character arrays, and null for objects). You also can create and initialize an array at the same time. Instead of using new to create the new array object, enclose the elements of the array inside braces, separated by commas:

String[] chiles = { “jalapeno”, “anaheim”, “serrano”,“habanero”, “thai” };


Arrays are accesses with elements 0 through n-1 where n is the number of elements allocated in the array. "As you saw in the previous examples, you can assign a value to an array’s specific slot by putting an assignment statement after the array name and subscript, as in the following:

myGrades[4] = 85;

The following statements show how to declare and access these arrays of arrays:

int[][] coords = new int[12][12];
coords[0][0] = 1;
coords[0][1] = 2;