user391339
user391339

Reputation: 8775

In java, what is the benefit of initializing an array using "new"?

For example, one might initialize a character array as:

char[] myArray = new char[3];
myArray = {'a', 'b', 'c'};

Or one might initialize the character array as:

char[] myArray = {'a','b', 'c'};

What is the benefit of using the "new" methodology ?

Incidentally, in the first example, why am I also allowed to assign the { ... } set without passing it to a constructor method (in parenthesis)?

char[] myArray = new char[] {'a', 'b', 'c'};

Upvotes: 1

Views: 464

Answers (6)

Stephen C
Stephen C

Reputation: 719109

This statement is not legal Java:

    myArray = {'a', 'b', 'c'};

The { ... } syntax can only be used as an initializer in a declaration, or in an array new expression. As the JLS section §10.6 says:

"An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values."

So the choice is actually between

char[] myArray = {'a','b', 'c'};

and

char[] myArray = new char[] {'a', 'b', 'c'};

The difference between the two forms is purely syntactic. They mean the same thing and I would expect them to compile to identical code in this example. The only practical difference is that the form with an explicit new can be used in contexts where the other one cannot.


Incidentally, in the first example, why am I also allowed to assign the { ... } set without passing it to a constructor method (in parenthesis)?

Because Java array types don't have constructors.

Upvotes: 1

Thilo
Thilo

Reputation: 262644

If you don't know the length at compile-time, you cannot use the literal syntax.

char[] buffer = new char[bufferSize];

Also, if the default value (0) is fine, you don't need the literal syntax.

// hard to read, and I probably got the number of entries wrong
char[] buffer = { 0,0,0,0,0,0,0,0,0,0,0,0 };

// easier on the eyes
char[] buffer = new char[10];

As to why you are allowed to skip the type declaration on the "constructor" (not sure if that is the proper term for arrays), that is because the compiler can infer it.

Upvotes: 1

Ramon
Ramon

Reputation: 8424

I think even when the terser syntax is legal, using the new syntax is preferable because it makes it clear that a new object is being created each time the statement is executed.

Upvotes: 1

benz
benz

Reputation: 4629

The main benefit of using new with array is when you know the size of the array but you want to initialize with elements later. In the second example you know what you want to store in the array so there is no need of new at that time. These are basically two legal ways of declaring the array. HTH, Ben

Upvotes: 1

Makoto
Makoto

Reputation: 106460

Your first statement is illegal - you cannot instantiate and populate an array using that shortcut on different lines.

As for using new - this allows you to fill in the values when you get them. Your only known would be at least one dimension of length for the array. Using the brackets implies that you know both the dimension and what will be placed into the array at instantiation.

Upvotes: 1

Amit Deshpande
Amit Deshpande

Reputation: 19185

{and} is a Short cut syntax and here the length of the array is determined by the number of values provided between { and }.

From Arrays Oracle docs

One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for ten integer elements and assigns the array to the anArray variable.

// create an array of integers
anArray = new int[10];

Alternatively, you can use the shortcut syntax to create and initialize an array:

int[] anArray = { 
100, 200, 300,
400, 500, 600, 
700, 800, 900, 1000
};

Upvotes: 2

Related Questions