DIBits
DIBits

Reputation: 533

What type of syntax is the Java "new Type[]{...}" array literal initialization?

What grabbed my attention, which I cannot explain to myself, is a thought about this well known code:

String[] str = new String[]{"a","b","c"};

Is new String[] a cast? If it is, why do we use new and no brackets? We would cast as in:

float i = (float) 3;

It also seem not to be a constructor, because then we would use it like a function call (e.g new String[](...)).

So what kind of syntax is it, and do we have more of that kind in Java?

Upvotes: 0

Views: 170

Answers (4)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26946

The syntax is related to the array initializer:

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

Basically you not only create the array, but also initialize all its fields in the same instruction.

It is not a cast.


Note that the code:

String[] str = new String[]{"a","b","c"};

is a single command to create and initialize the array but it is also possible to use a less verbose version:

String[] str = {"a","b","c"};

Upvotes: 1

Eran
Eran

Reputation: 394146

This syntax is an example of an 10.6. Array Initializer as part of an 15.10.1. Array Creation Expression.

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

new String[]

is an array creation expression and

{"a","b","c"}

is an array initializer.

Since there are no dimension expression in your array creation expression (i.e. nothing inside the square brackets), there must be an array initializer:

If there are no dimension expressions, then there must be an array initializer. A newly allocated array will be initialized with the values provided by the array initializer as described in §10.6.

Upvotes: 3

A_C
A_C

Reputation: 925

Arrays can be created in multiple ways like below, and you are using the second one which is called the Array Initializer where you create the array while also initializing it.

int[] abc = new int[3];   // This means an array of integers with size 3 is created.

int[] def = new int[]{1,2,3};   // This means an array of integers with size 3 is created and also initialized with the values 1, 2 and 3.

In the second statement, I am creating an array of integers with the elements as 1, 2 and 3 where the size is implicitly 3.

So, in your case String[] str = new String[]{"a","b","c"}; , this statement is creating an array of String values with elements "a", "b" and "c" with the implicit size of the array being 3 due to the 3 elements which it is initialized with.

Upvotes: 0

OhleC
OhleC

Reputation: 2950

It's called an Array Initializer, and its sole purpose, as the name suggests, is to initialize arrays.

Upvotes: 2

Related Questions