• 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

what difference does it make ...arrayini

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in class below..,
1) what is the diff between
int anar[]=new int[]{1,2,3};
int anar[]={1,2,3};
2)i thought "int anar[]=new int[]{1,2,3};" would give a compiler err, does the above statement signify double dimension array...?
public class arrayini {
public static void main(String argv[]){
int anar[]=new int[]{1,2,3};
int anar[]={1,2,3};
System.out.println(anar[1]);
}
}
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1). There isn't a differnce. The second option is just a shorter version of doing it.
2).That is not a multi-dimensional array. A multi-dimensional array would look something like this:
int []i[]={{},new int[]{}};
or even:
int i[][]={{1,2,},new int[2]};
But you really needn't worry about those right now.

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srini
There is not much difference in the two syntax. For the second one compiler provides you with a new for creating the array. You can use second form of initialization only while declaring the array.
The first form can be useful in passing arrays to the methods. For example if your method is
aMethod(Object[] a)
you can call
aMethod(new String[]{"one","two","three"});
aMethod(new Integer[]{new Integer(1), new Integer(2)});
etc.

[This message has been edited by Anshul Manisha (edited July 08, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic