aspose file tools*
The moose likes Beginning Java and the fly likes what does (String[ ] args) mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what does (String[ ] args) mean?" Watch "what does (String[ ] args) mean?" New topic
Author

what does (String[ ] args) mean?

Mike Hudek
Greenhorn

Joined: Dec 31, 2005
Posts: 17
Hello,

Just getting started in Java - and OO programming in general.

I'm not sure what the (String [] args) part of the code in the line

public static void main (String [] args)

mean?

The "Head First Java" explanation is "
arguments to the method. This method must be given an array of Strings, and the array will be called 'args'
.", but this isn't entirely clear to me.

If anyone could shed a bit more light on the meaning of this syntax so that a complete newbie can get some feel as to what is going on, it would be much appreciated.

Regards,

Mike


Mike
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

When you call a Java main, you can put some arguments i with the call.

e.g.


When you call with arguments, you can get the values this way:


Ren´┐Ż
[ January 14, 2006: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Hi, Mike

Well, any Java program needs a starting point. When a class has a method with the signature public static void main(String args[]) that means that class is elegible as an application starting point.

For instance:



After compiling your class with the javac.exe (in Windows) or the javac (Linux) command you can execute because this class is elegible as the application starting point, and you can make this class begin execution by means of using the java.exe (in Windows) or the java(in Linux) command that comes with the JDK and that it is stored in the bin directory along with other toys.

If you are using an IDE (Integrated Development Environment) this details might be hidden from you.

But you can do it like this:

1. TO COMPILE THE CLASS



2. TO EXECUTE THE CLASS



Finally If you want your application to receive parameters from the outter world, those parameters will be received in the args[] argument of the public static void main(String args[]).

As in this another case, where you will be able to pass the name of the Jedi that you want to be printed on the screen:



And, after compiling again, you call that program with



I hope all this helps

Regards,
Edwin Dalorzo
Mike Hudek
Greenhorn

Joined: Dec 31, 2005
Posts: 17
Thanks very much for your help. Much appreciated!

Mike
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169
Hi Mike,

you might have noticed that some people write "String[] args", some "String args[]".

Both mean the same, but can be read differently:
"String[] args" (as a method parameter) means, "pass me something, this something is going to be an array of Strings (String[]) and will be called args in the rest of the method

"String args[]" (as a method parameter) means, "pass me something, this something is going to be a String, but one moment it looks like its going to be a load of Strings in an array that will be called args in this method"

As you see, it may be simpler to use the first syntax, as it is clearer for the reader/programmer/maintenance person.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Stuart Goss:
Hi Mike,

you might have noticed that some people write "String[] args", some "String args[]".

Both mean the same, but can be read differently:
"String[] args" (as a method parameter) means, "pass me something, this something is going to be an array of Strings (String[]) and will be called args in the rest of the method

"String args[]" (as a method parameter) means, "pass me something, this something is going to be a String, but one moment it looks like its going to be a load of Strings in an array that will be called args in this method"

As you see, it may be simpler to use the first syntax, as it is clearer for the reader/programmer/maintenance person.


The second version is comes from C/C++. For those of us that have programmed in C previously, either way is just as clear.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what does (String[ ] args) mean?