• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what does (String[ ] args) mean?

 
Mike Hudek
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for your help. Much appreciated!

Mike
 
Stuart Goss
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic