Creating Arrays in Java

Simple data types of the kind we saw in the previous section are fine for storing single data items, but data is often more complex. Like JavaScript, Java supports arrays as well. Here's an example; in this case, I'll store the balances in customers' charge accounts in an array named chargesDue. I start by declaring that array, making it of type double:

public class app
{
    public static void main(String[] args)
    {
        double chargesDue[];
    .
    .
    .

Before using the array, you also have to allocate the number of elements that you want the array to hold. You do that like this, using the new operator:

public class app
{
    public static void main(String[] args)
    {
        double chargesDue[];

        chargesDue = new double[100];
    .
    .
    .

You can combine ...

Get Inside XML 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.