Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Fendel Coulptz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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...
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • 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 ]
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks jason n nigel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic