• Post Reply Bookmark Topic Watch Topic
  • New Topic

Different ways of Array creation  RSS feed

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

What is the difference between these two array creations:
int[] array = {1,3,4,5}; //This works fine.

int[] array;
array = {1,3,4,5};//This gives an error as "illegal start of expression".
Even though the array is declared in the first line why this is giving an error.
Can any one explain the reason for this.

Thanks,
Ravindra.
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Me too I would like to know the reason. I try an approach.

int[] array1;
array1 = {1,3,4,5};

The first line tells, there is an array of int called array1. There is no memory allocation.
If there is a semicolon at the end of a line, it is an expression. The second line has a semicolon at the end. The compiler tells: illegal start of expression.

Who can continue to develop the thoughts?

Regards
Urs
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

in this statement: int[] array = {1,3,4,5}; you declare and construct an array with the size of 5.

In this statement: int[] array you declare an integer array.
This statement: array = {1,3,4,5}, expects that the right hand side
(the {1, ..} part) is a construction of the array.

/Svend Rost
 
Marshal
Posts: 57269
175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this instead:-That compiles and runs nicely
 
lowercase baba
Bartender
Posts: 12592
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The real question is not why the second way DOESN'T work, but why the first way DOES work.

This is a convenient shortcut the fine people at Sun gave us. I believe it is a leftover from C. Very few objects have shortcuts to create them - arrays and Strings are the only two i can think of off the top of my head.
[ March 21, 2007: Message edited by: Fred Rosenberger ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Svend Rost:
Hi,

in this statement: int[] array = {1,3,4,5}; you declare and construct an array with the size of 5.

/Svend Rost



Svend,

Why does this contruct an array with the size of 5? Why wouldn't it create an array with the size of 4, since 4 elements are specified?
[ March 22, 2007: Message edited by: James A Anderson ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12592
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
methinks Svend just made a typo...
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Very few objects have shortcuts to create them - arrays and Strings are the only two i can think of off the top of my head.

and with 1.5 the wrapper classes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!