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 ch10_04 
{
    public static void main(String[] args)
    {
        double chargesDue[];
    .
    .
    .

Besides declaring the array, you have to allocate the number of elements you want the array to hold. You do that using the new operator:

public class ch10_04 
{
    public static void main(String[] args)
    {
        double chargesDue[];
        chargesDue = new double[100];
    .
    .
    .

You can combine the ...

Get Real World 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.