Top
Previous Next
Java Arrays, Objects, Methods CS 161 - Java

Arrays Can Be Made of Any Type or Class

"Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array."
from: Java Language Specification, Gosling, Joy, and Steel, 1996

Examples

int[] intArray = new int[4]; // elements initially set to 0

CreditCard cards[] = new CreditCard[MAXCARDS];
                  // elements initially set to null
                  // notice the [] can be placed with the field name
                  // or the type; though the latter is "better"


Tip: If you need a colllection's size to change consider using java.util.Vector or another collection class instead of arrays
Top
Previous Next
jwd