wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what difference does it make ...arrayini Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what difference does it make ...arrayini" Watch "what difference does it make ...arrayini" New topic
Author

what difference does it make ...arrayini

srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
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]);
}
}

please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
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.

Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
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).]


AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
 
Don't get me started about those stupid light bulbs.
 
subject: what difference does it make ...arrayini
 
Similar Threads
array!
mock exam question
doubt(marcus mock test)
Why this code does not generate any exception?
Dharmesh Rathod (Info)