Simple java arrays question

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Why is it that you can do this

Code:
int [] myArray1 = {0,1,2,3,4,5};

But you can't do this

Code:
int [] myArray1;
myArray1 = {0,1,2,3,4,5};

Cheers,
Paul
 
Code:
int [] myArray1 = {0,1,2,3,4,5};
You are declaring an array, and also declaring you want enough memory for the set {}

Code:
int [] myArray1;
myArray1 = {0,1,2,3,4,5};

You declare the array (just a reference), then you are trying to use the array without allocating memory.
 
Another explanation is that the top one is the shorthand version, and your breaking the shorthand rule as such. Jim's answer is the technically correct answer however :)

Do you actually have a problem you think you need to use the 2nd (incorrect) form with?
 
Back
Top Bottom