Two Laptop Bag
The moose likes Beginning Java and the fly likes why always Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why always "string args[ ]"  " Watch "why always "string args[ ]"  " New topic

why always "string args[ ]"

vivek mehta

Joined: Feb 20, 2001
Posts: 3
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"

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: why always "string args[ ]"
It's not a secret anymore!