| Author |
String array Vs String var args
|
Krishan Chauhan
Ranch Hand
Joined: Mar 12, 2008
Posts: 32
|
|
Hi Could anyone tell me the difference between these: public static void main(String[] args){} public static void main(String... args){} Is it like that String array and String var args means the same thing? Thanks Krishan
|
 |
K Kiran Kumar
Ranch Hand
Joined: Jan 04, 2006
Posts: 109
|
|
In public static void main(String[] args), all the command line arguments were taken in args. like: java somefile abc xyz Here args[0]=abc args[1]=xyz But public static void main(String s1) is a normal static method. How come both will be the same....? I mean args[] is an array and args is a single String obj variable. Did I answer your question?? [ May 09, 2008: Message edited by: K Kiran Kumar ]
|
 |
Wirianto Djunaidi
Ranch Hand
Joined: Mar 20, 2001
Posts: 195
|
|
Krishan, When you see it from java application starting up as the point of entry then the public static void main() method, either String[] or String... is similar. It make a different if you use it in regular method or call main from other method. With String[] signature when calling it you'll have this: when you have varargs you'll call it like this: Does that help? -DJ
|
 |
Krishan Chauhan
Ranch Hand
Joined: Mar 12, 2008
Posts: 32
|
|
Hi DJ Thanks for your reply. Do you mean to say that we can have something like this: void callMe(int[] a){} void callMe(int... b){} Java treats both of them as duplicate method. Pls tell in what context they are different. Thanks Krishan
|
 |
Venkata Kumar
Ranch Hand
Joined: Apr 16, 2008
Posts: 110
|
|
Krishan, Both the methods are identical for java compiler. The difference is that the way we can call these methods. The method void callMe(int... b){} can be called as The method void callMe(int[] a){} can be called as -Sai
|
SCJP 5.0, SCWCD 5, preparing for SCDJWS
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
This article will help you understand better. This line from the article explains a lot:
It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process.
So, the compiler behind the scenes actually converts your var args method to a method with an array input. This is the reason why you can have a var args method overloaded with an array as input because after compilation both of them will be identical.
|
apigee, a better way to API!
|
 |
arulk pillai
Author
Ranch Hand
Joined: May 31, 2007
Posts: 3185
|
|
You can invoke with different number of arguments
|
Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
|
 |
Krishan Chauhan
Ranch Hand
Joined: Mar 12, 2008
Posts: 32
|
|
Thanks All It was really helpful.
|
 |
 |
|
|
subject: String array Vs String var args
|
|
|