Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String array Vs String var args

 
Krishan Chauhan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
arulk pillai
Author
Ranch Hand
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can invoke with different number of arguments


 
Krishan Chauhan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All
It was really helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic