Results 1 to 7 of 7
- 02-21-2010, 09:34 PM #1
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Passing values to main(String args[]) ?
Hi, I saw some code which appeared to pass values to void main(String args[])
and I was wondering how this is done and why it would be done.
example:
Also, where exactly in code do you call the void main(String args[]) function. :confused: I have never seen anything like for example:Java Code:public static void main(String args[]) { if (args.length < 2) { System.out.println("some message"); }
Thanks in advance for any help :)Java Code:String str="hello"; if(someCondition==true) { main(hello); }
-
If you have a program Hello.java which you compile to Hello.class, you pass these parameters (known as command line parameters) when you call the program:
c:\java>java Hello this is a command line parameter
java Hello is calling your program and all that follows are the command line parameters.
- 02-21-2010, 09:44 PM #3
Senior Member
- Join Date
- Mar 2009
- Location
- USA
- Posts
- 127
- Rep Power
- 0
when you can any java file from command line you can pass arguments. If length of the command line arguments is less than 2 then it will print that message.
- 02-21-2010, 09:59 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Ok so let me see if I understand. If I were to write c:\java>java Hello
then args[0] would contain nothing?
but if I were to write c:\java>java Hello msg
then args[0] would be msg.
I have just used run configuration in eclipse and used (x)=Arguments option and wrote something in there and ran the program and args.length printed 1. Is this the same idea as the command line parameter pass?
-
- 02-21-2010, 10:15 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Thanks Fubarable.
- 02-21-2010, 10:16 PM #7
Hallo Marshy
if you don't pass any parameters and you ask for args[0] then you will get a OutOfBoundsExeption. So, to avoid this, use this for-example
Even if no parameter are passed no exception will be thrown and you don't have to care how many parameters are passed, because all will be processed in the for-loop.Java Code:for (int i = 0; i < args.length; i++) { System.out.println(args[i]); }
Similar Threads
-
String, quotes and args
By mac in forum New To JavaReplies: 8Last Post: 02-04-2010, 04:24 PM -
Public static void main (String args[])
By arefeh in forum New To JavaReplies: 12Last Post: 01-28-2010, 11:58 AM -
passing values from main page to pop up window
By vicky in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 12-29-2008, 03:06 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


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks