• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

dimension expression vs array initializer

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Morning Group (at least where I am),

I am chasing my tail on a simple concept.

Dimension expression vs array initializer.

"An array creation expression must have either a dimension expression or an initializer. If both are present, then a compile-time error is generated." -Dan Chisholm.

Example 1: int[] a1 = new int[];
Example 2: int a2[] = new int[5];

Why is Example 1 correct and Example 2 not?

Thanks for your help,

Jerry
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check that.

Example 1 is incorrect and Example 2 is correct.

Still need your help.

Thanks again,

Jerry
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Morning Jerry,
You should read the JLS for more information on declaring arrays. Maybe the following list will help, i used that myself when learning about arrays:

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jerry,
First, be clear with what is an Dimension expression & an initializer ?

DIMENSION EXPRESSION:
consider,
int[] a = new int[10];

here, the expression "new int[10]" is called the dimension expression. It creates an array of size 10.

the thumb rule is: SIZE OF ARRAY MUST BE SPECIFIED WHEN WE CREATE IT.

INITIALIZER:
consider,
int[] a = {1,2,4};

here, the expression "{1,2,4}" is called the initializer. It creates an array of size 3. what? But where is size mentioned ? that's the trick .. the size is automatically taken from the number of elements within { }.

So, now read dan's sentence ..what he really means is that we cannot have a stmt like following :

int[] a = new int[5] {1,2,3,4,5};

Now, can u understand that the examples u have posted are also irrelevant !!!

example 1 is wrong because size is mandatory. Not because of dan's statement.

bye,
senthil.
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SenthilKumar and Tom,

Thank you for taking the time to straighten me out.

:-)

Jerry Bustamente
 
reply
    Bookmark Topic Watch Topic
  • New Topic