• 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

Arrays question.

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My previous post was on arrays too.. seems like I'm gonna be stuck there forever! Anyways, please have a look at the following question.

Question:




Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5


The answers are 'a' and 'e'. I would like to know why isn't an error generated while compiling lines 3 and 4.

TIA,
~D
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
(3) and (4) are the other ways to define and initialize the arrays.
Right Hand side of (3) namely new int[]{1, 2} goes by the name anonymous array. KB book talks about this in depth.

Thanks
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3. int[] a3 = new int[]{1,2};
Arrays can defined as [] on left or right of array name.
new int[] {1,2} means, two elements are passed to integer array, creating array length of two as a3[0]=1 and a[1]=2.

4.int []a4 = {1,2};

Array type is defined on left of operand "=". {1,2} can assign elements directly to array. This is another way of assigning array element values.

Hope this helps
 
Dee Irun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you PremKumar and Nitin!

~D
 
reply
    Bookmark Topic Watch Topic
  • New Topic