Results 1 to 8 of 8
- 08-18-2009, 10:34 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
-
This allows one to pass argument strings to your program when you start it. For instance if you have a class called MyProgram.java with a main method, and you then compile it to MyProgram.class, and finally if you run this like so:
The two strings foo and bar are passed to the main method within the args array in positions args[0] and args[1].Java Code:java MyProgram foo bar
- 08-18-2009, 10:56 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
ahh So I can wright java myapp hello
And the funtion is:
can I?Java Code:class test{ public static void main(String args[]){ System.out.println(args[0]); } }
-
sort of, for that since the class is called test, you'd have to call
Java Code:java test hello
- 08-18-2009, 11:21 PM #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
you'd also need to make sure that args[0] exists. you might have args of size 0. unlike c, the program name is NOT the first arg
- 08-18-2009, 11:40 PM #6
Member
- Join Date
- Jun 2009
- Posts
- 3
- Rep Power
- 0
Interesting! so that's what it's used for.
I was wondering what the String args[] purpose was as well, and why it was needed in every main method.
- 08-19-2009, 10:19 AM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
But it is not needed unless ur gona put in some input! Right?
And can I do (int args[]) and execut by typing:
test 54
?
- 08-19-2009, 10:24 AM #8
Similar Threads
-
[SOLVED] [newbie] PrintStream printf(String fmt, Object... args)
By jon80 in forum New To JavaReplies: 6Last Post: 05-19-2009, 12:22 PM -
String[] arg and String... args
By mahendra.athneria in forum Advanced JavaReplies: 4Last Post: 04-28-2009, 07:20 PM -
Error: LengthCharAt.java:3: ';' expected public static void main (String[] args)
By antgaudi in forum New To JavaReplies: 9Last Post: 11-22-2008, 11:03 PM -
Why can't we write main without String args[]
By shailender in forum New To JavaReplies: 4Last Post: 11-05-2008, 10:58 AM -
[SOLVED] ReadLine(String fmt,Object... args) of Console class
By Pooja Deshpande in forum New To JavaReplies: 4Last Post: 04-25-2008, 05:51 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks