It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Different ways of Array creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Different ways of Array creation" Watch "Different ways of Array creation" New topic
Author

Different ways of Array creation

Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
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.
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
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


SCJP 1.4
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Try this instead:-That compiles and runs nicely
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11164
    
  16



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 ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James A Anderson
Greenhorn

Joined: Mar 02, 2007
Posts: 12
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

Joined: Oct 02, 2003
Posts: 11164
    
  16

methinks Svend just made a typo...
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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.
 
subject: Different ways of Array creation
 
Similar Threads
Array dimension missing
Qauestion By JDiscuss
What can be thrown
Doubt in Array Declaration
SJCP question .