Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why always "string args[ ]"

 
vivek mehta
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we always use "string"?Can't we use int , char,etc....?
Explain please also the significance of square braces after args.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When SUN created Java, they sat down and said "OK, what is the ONE best signature that we can use as a starting point for ALL applications".
They decided that String can encompass all other data types as input. So they coded the compiler to always look for that particular method signature, and when found, use it to kick off the program.
public static void main(String args[]){}
is the chosen signature. The brackets are by the args because it is an array of String parameters that are coming into the program.
So if I type at the prompt
>java MyProgram one 345 76.45 cindy
main() will start MyProgram and the 4 parameters following it are available in
args[0] = "one"
args[1] = "345"
args[2] = "76.45"
args[3] = "cindy"
Then it is up to the programmer to deal with those parameters in some meaningfull manner.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic