File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes String array Vs String var args Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String array Vs String var args" Watch "String array Vs String var args" New topic

String array Vs String var args

Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32

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?

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
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: 210


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?
Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32

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.

Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110


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


SCJP 5.0, SCWCD 5, preparing for SCDJWS
Nitesh Kant

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
Ranch Hand

Joined: May 31, 2007
Posts: 3359
You can invoke with different number of arguments

[] | [Java job hunting know how] | [Java resumes]
Krishan Chauhan
Ranch Hand

Joined: Mar 12, 2008
Posts: 32
Thanks All
It was really helpful.
I agree. Here's the link:
subject: String array Vs String var args
It's not a secret anymore!