Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 437
Taylor Lopez Avatar asked Oct 02 '14 20:10

Taylor Lopez


People also ask

What happens when you initialize an array in Java?

Array Initialization in Java This allocates the memory for an array of size 10 . This size is immutable. Java populates our array with default values depending on the element type - 0 for integers, false for booleans, null for objects, etc.

What happens if an array element is not initialized in Java?

Even if you do not initialize the array, the Java compiler will not give any error. Normally, when the array is not initialized, the compiler assigns default values to each element of the array according to the data type of the element.

What happens when you initialize an array?

Initialization of multidimensional arrays Listing the values of all elements you want to initialize, in the order that the compiler assigns the values. The compiler assigns values by increasing the subscript of the last dimension fastest.


1 Answers

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.

like image 124
5gon12eder Avatar answered Oct 05 '22 23:10

5gon12eder