简体   繁体   中英

If you're explicitly initializing an Object array in Java, is including “new Object[]” different than not including it?

Preface: this question has been asked here , but I'm wondering specifically about the author's specific meaning.

I'm reading through Thinking in Java, 3rd ed. Revision 4.0 , and Eckel shows this snippet in Chapter 4, Initialization and Cleanup :

public class ArrayInit
{
  public static void main(String[] args)
  {
    Integer[] a =
    {
      new Integer(1),
      new Integer(2),
      new Integer(3),
    };

    Integer[] b = new Integer[]
    {
      new Integer(1),
      new Integer(2),
      new Integer(3),
    };
  }
}

And states the following:

The first form is useful at times, but it's more limited since the size of the array is determined at compile time.
The second form provides a convenient syntax to create and call methods that can produce the same effect as C's variable argument lists (known as “varargs” in C). These can include unknown quantities of arguments as well as unknown types.

I've never known these to be different as Eckel describes. To my understanding, they are both arrays of static size. I don't understand how the first is any more "limited" than the second.

What's he talking about?

I think this might be what the author is referring to.

Since Java 5, we can declare functions with variable argument lists .

public static int newStyleSum(final int... numbers) {
    int sum = 0;
    for (final int number : numbers) {
        sum += number;
    }
    return sum;
}

They can be used as in:

int s = newStyleSum(1, 2, 3, 4);

This feature is merely syntactic sugar. Internally, an anonymous array is passed to the function.

Before we had this syntax, the above example would have to be written as:

public static int oldStyleSum(final int[] numbers) {
    int sum = 0;
    for (int i = 0; i < numbers.length; ++i) {
        sum += numbers[i];
    }
    return sum;
}

and called as

int s = oldStyleSum(new int[]{1, 2, 3, 4});  // "second" form

but not as

int s = oldStyleSum({1, 2, 3, 4});  // "first" form (syntax error)

which, even today, is still a syntax error.

And that might indeed be what he is talking about. Java 5 came out in 2004 so for a 2002 book, it makes sense.

The new syntax is more flexible and – importantly – backwards compatible, so we can still do

int s = newStyleSum(new int[]{1, 2, 3, 4});

or, more importantly,

int[] numbers = {1, 2, 3, 4};
int s = newStyleSum(numbers);

if we want to.

It seems he is referring to the array initializer, specifically new Integer[] . From the same chapter using the explicit initializer is given

Other.main(new String[]{ "fiddle", "de", "dum" }); 

to show the usefulness of passing an array to a method which is not possible without the array initializer expression

You're right, it doesn't make any sense.

Here's what rev 2 used to say :

This is useful at times, but it's more limited since the size of the array is determined at compile-time. The final comma in the list of initializers is optional. (This feature makes for easier maintenance of long lists.)

"This" here refers to "initialize arrays of objects using the curly-brace-enclosed list", which is true. It looks like someone made a bad edit, thinking "this" referred to "the first form".

It goes on to say that the second form is a useful way of simulating varargs, which was true at the time, but Java has since gotten actual varargs . (The first form can only be used in array declarations, not in method calls, and is therefore not useful for varargs).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM