Reputation: 379
Please explain to me what the difference is between the two declaration of an array
int[] a = {5, 7, 8, 9, 6};
and
int[] arr = new int[]{5, 7, 8, 9, 6};
Is it something like in first statement there is no memory allocation and in the second memory is allocated to each element?
Also, why does the below declaration result in an error?
int[] arr = new int[5]{5, 7, 8, 9, 6};
Upvotes: 3
Views: 135
Reputation: 3698
There is no difference between the first two, you will allocate memory for them as they both require.
The third one, however, is incorrect as it has duplicated information. If you are using {x,y,z}, the number of elements is implied and you don't need or should repeat this information on [number_elements]. Especially because those peaces of information could be incoherent as: [2]{x,y,z}
from oracle's docs pages:Here the length of the array is determined by the number of values provided between braces and separated by commas
That is also why it is perfectly fine to do int[]{5,7,8,9,6}
Now, if you don't have the initial values on a {}, there is no information; thus the need for [number].
Upvotes: 0
Reputation: 106508
They may look similar, and they may behave similarly, but they are indeed different.
The first declaration you have is an array initializer. It can only be used when you're declaring a field or variable (as you are with int[] a = {1, 2, 3, 4, 5, 6}
), or as part of the second form, an array creation expression.
The array initializer requires a reifiable type in order for it to be valid syntax. This means that you cannot use it to create a generic array. As an example:
public class Example<T> {
public void doStuff(T first, T second, T third) {
// Invalid; T is not a reifiable type
T[] stuff = {first, second, third};
}
public void doStuff(int first, int second, int third) {
// Valid; int is reifiable
int[] stuff = {first, second, third};
}
}
The second declaration that you have is an array creation expression. It allows for several forms of declaring an array. It also allows you to create generic arrays through some rather forced casting.
To the question of why the third syntax is invalid: below is an excerpt of the ArrayCreationExpression.
ArrayCreationExpression: new PrimitiveType DimExprs Dims (optional) new ClassOrInterfaceType DimExprs Dims (optional) new PrimitiveType Dims ArrayInitializer new ClassOrInterfaceType Dims ArrayInitializer DimExprs: DimExpr DimExprs DimExpr DimExpr: [ Expression ] Dims: [ ] Dims [ ]
Effectively, the reason the syntax new int[5]{1, 2, 3, 4, 5}
is invalid is because of the above.
int[5]{1, 2, 3, 4, 5}
contains a DimsExpr
, which is the [5]
piece, and an ArrayInitializer
, which is the {1, 2, 3, 4, 5}
piece. The above language specification does not allow for both a DimsExpr(s)
and an ArrayInitializer
to be declared together.
Upvotes: 2
Reputation: 11209
No difference between the two first declarations. The last declarations specifies the length of the array twice. I assume that it is invalid to avoid having the compiler match the two specified sizes.
Upvotes: 0