1

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'};
1
  • You don't always know the contents of your array beforehand Commented Sep 11, 2012 at 5:13

6 Answers 6

2

{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
};
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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

Comments

1

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.

Comments

1

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.

1 Comment

Using objects in the shortcut syntax declaration works fine - it's not limited to just primitives.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.