O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required