Creating Arrays

You can declare a new array variable, just like you would any other member variable (like this)

String[] args;

The square brackets mean “array.” An array in Java is an object, which means that you initialize an array with the keyword new. Like this:

String[] args = new String[10];

This creates a new array object, called args, with 10 buckets for putting Strings into. You can fill an array with whatever kind of thing you want: Object, int, boolean, Address, char, another array, and so on.

The first element of a Java array has an index of 0. To reference the element in the second bucket of an array named args, type this:

args[1]

FRIDGE

Remember that arrays are the size that you make them, no bigger, and that the first element's ...

Get Java Garage 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.