| Author |
main() parameter construction
|
Raj chiru
Ranch Hand
Joined: Aug 12, 2008
Posts: 140
|
|
Hi...
what is meaning of this type of parameter construction i.e public static void main(String... args)
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
|
Try googling varargs in Java. Its a way to specify multiple parameters to method .
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8435
|
|
Those are called Varargs.
More here
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Matthew Cox
Greenhorn
Joined: May 25, 2009
Posts: 29
|
|
This is done to provide greater flexibility for the language at runtime. You can pass in parameters to the program which are stored in the String[] args variable.
A good way to introduce this concept is examining the notion of a Windows Shortcut. If you check the properties of your Internet Explorer icon, note the Target:
It should look something like the following:
"C:\program files\internet explorer\iexplorer.exe"
Now, I can add parameters to the end of the target to tell the icon to not only load the exe, but pass in string parameters as well!
"C:\program files\internet explorer\iexplorer.exe" www.coderanch.com
So, now this icon could launch the iexplorer.exe and pass it the coderanch website address to load immediately.
In this same way, executable java applications can have parameters passed in, via any interface that supports parameter passing.
The executable jar file would have to have parameters attached to it for this example to work correctly but hopefully you see the benefits. This is especially nice when your application needs to launch another application and pass specific parameters for a desire effect.
//Note: ExecutionObject is black-boxed in the example, assume it is programmed to allow a given exe file to be executed via its String path in the O/S dir.
hope this makes sense.
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Matthew, I think the original poster is more concerned with what's varargs means, like How one can define "psvm(String... args)". But your explanation was great
|
 |
Matthew Cox
Greenhorn
Joined: May 25, 2009
Posts: 29
|
|
Sagar Rohankar wrote:Matthew, I think the original poster is more concerned with what's varargs means, like How one can define "psvm(String... args)". But your explanation was great 
Ahhhh lol! I should have read the article before posting doh! Well I didn't even know that Java had added a feature like that but nice to know =D
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
if (args[0] instanceof String) . . .
I challenge you to find any circumstances where that will return false
|
 |
Matthew Cox
Greenhorn
Joined: May 25, 2009
Posts: 29
|
|
Campbell Ritchie wrote:
if (args[0] instanceof String) . . .
I challenge you to find any circumstances where that will return false
Haha it's late lol.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
But you might still suffer an ArrayIndexOutOfBoundsException.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
Campbell Ritchie wrote:
if (args[0] instanceof String) . . .
I challenge you to find any circumstances where that will return false
TheClass.main(new String[] { null } );
OK, so what do I win?
|
[Jess in Action][AskingGoodQuestions]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
Damn! I forgot about that.
1st prize, Ernest: A day-trip to [fill in town name]
2nd prize: A two week holiday in [same location]
3rd prize: A house at [same location]
|
 |
 |
|
|
subject: main() parameter construction
|
|
|