• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

total newbie to arrays: don't really understand main(String[] args)

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why "String[] args" in main(String[] args)?
i dun get it... why do we use it for all tha main() methods and how does it work?

thanks in advance...
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Real quick, the String[] args part of main means to pass in an 'array of strings'. See About's Java Array Tutorial for a better explanation of arrays than I could give. Read the tutorial (it is vital to understand arrays in Java), then come back to this post.

By allowing an array of strings to be passed from the commandline, Java allows for 'arguments' to be passed. For example:
java MyArgumentExample jason fox

passes the Strings "jason" and "fox" to my java program, to do with as I please. Quick example in action:


If run with the command ' java Example word' will print 'word', or whatever else you pass as an argument.
[ September 28, 2004: Message edited by: Jason Fox ]
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java when an application in interpreted by the JVM it starts by calling, that classes 'main' method. The main method excepts an array of the type String as an argument.
Each String in this array is known as a 'command line argument' and through these arguments the runtime system can pass information into your application. A common usage of this feature would be to tell the application the position of a policy or properties file on a particular system. It should also be noted that these command line arguments are non-persistent, for example One could use a command line argument to turn logging on or off so depending on the argument passed to 'main', the application will behave differently.
 
Fendel Coulptz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks jason n nigel
 
Everybody's invited. Even this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic