• Post Reply Bookmark Topic Watch Topic
  • New Topic

main() parameter construction  RSS feed

 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...
what is meaning of this type of parameter construction i.e public static void main(String... args)
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try googling varargs in Java. Its a way to specify multiple parameters to method .
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are called Varargs.
More here
 
Matthew Cox
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (args[0] instanceof String) . . .


I challenge you to find any circumstances where that will return false
 
Matthew Cox
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you might still suffer an ArrayIndexOutOfBoundsException.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!