• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

main() parameter construction

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...
what is meaning of this type of parameter construction i.e public static void main(String... args)
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try googling varargs in Java. Its a way to specify multiple parameters to method .
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those are called Varargs.
More here
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you might still suffer an ArrayIndexOutOfBoundsException.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
reply
    Bookmark Topic Watch Topic
  • New Topic