Results 1 to 13 of 13
- 01-26-2010, 03:14 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
Public static void main (String args[])
HelloJava Code:class Example1 { public static void main(String[] args) { int temperature = 0; if (args.length > 0) { try { temperature = Integer.parseInt(args[0]); } catch(NumberFormatException e) { System.out.println( "Must enter integer as first argument."); return; } } else { System.out.println( "Must enter temperature as first argument."); return; }
1-what is args in String[] args?
2-why is main static?
3-why is args.length always zero?
- 01-26-2010, 04:00 PM #2
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
1-what is args in String[] args? it is the default name of the variable (the arguments sent to the program via commandline ( or shortcut etc...) (hint, you can change the name))
2-why is main static? Because otherwise you would need to instantiate it ? TBH I can't answer that well, but basically I think it's just easier for the JVM, the way the devs wrote it...
3-why is args.length always zero? It's not, it's only zero if you call the program with no arguments.
try putting a for loop and then calling from commandline:
program hello bob, my name is joe...
for ( int i=0; i< args.length; i++ )
System.out.println(args [i]);
the output is:
hello
bob,
my
name
is
joe...
- 01-26-2010, 04:41 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
It's static because otherwise the JVM would have to instantiate an instance of <MainClass>, and therefore would have to know that MainClass had a default constructor...all to simply get the app started up. Whereas with a static main it knows that MainClass.main(args) should exist, and all it has to do is load the class.
- 01-26-2010, 04:57 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
Hello
Thank you very much
Also I found thease about public static void main(String args[])
The Main method is the method in which execution to any java program begins.
A main method declaration looks as follows:
public static void main(String args[]){
}
The method is public because it be accessible to the JVM to begin execution of the program.
It is Static because it be available for execution without an object instance. you may know that you need an object instance to invoke any method. So you cannot begin execution of a class without its object if the main method was not static.
It returns only a void because, once the main method execution is over, the program terminates. So there can be no data that can be returned by the Main method
The last parameter is String args[]. This is used to signify that the user may opt to enter parameters to the java program at command line. We can use both String[] args or String args[]. The Java compiler would accept both forms.
From: WikiAnswers - Public static void main String arg meansI asked that 3-why is args.length always zero?In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.
From: Concepts and Code for U: Public static void main (String args[])
And I entered on command line:
D:\jdk\bin>java Example1 3 2 5
and I did not get this message:
Must enter temperature as first argument.
Then I changed args[0] to args[1] in my program and wrote:
D:\jdk\bin>java Example1 3 2 5
the answer was the same.
Question: args[0] and args[1] are the same?
- 01-26-2010, 05:11 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
"This is necessary since main( ) is called by the Java interpreter before any objects are made."
That bit's not quite right since a Class is itself an object (of type Class, in fact). But I get what they're saying.
As for why you don't get that output, it's because you're not getting a NumberFormatException. And that's because 3 (which is arg[0]) and 2 (which is arg[1]) are both numbers.
- 01-26-2010, 09:09 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
OK.
1)with temperature = Integer.parseInt(args[0]);
-D:\jdk\bin>java Example1 a 3 b
Result: "Must enter integer as first argument."
2)with temperature = Integer.parseInt(args[1]);
-D:\jdk\bin>java Example1 3 a b
Result: "Must enter integer as first argument."
3)with temperature = Integer.parseInt(args[2]);
-D:\jdk\bin>java Example1 3 a b
Result: "Must enter integer as first argument."
Thank you.
Please describe it more.main(args) should exist, and all it has to do is load the class.
- 01-27-2010, 05:19 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
when instantiating an object of a class we use the 'new' keyword, such as Scanner.
Scanner keyboard = new Scanner(System.in);
This creates an object of type Scanner in which we can use all it's methods such as next(), nextInt(), hasNext() and so on.
However with static methods you can simply call on them using the dot notation. For instance this sample code:
So to call this method or more importantly while this is still a class and we can still instantiate an object of this class, using the object of the class we cannot call on it's static members.Java Code:public class Foo { public static void bar() { System.out.println("\'Foobar\' is actually spelled \'Fubar\'."); } // other non-static methods follow here ... }
To use static methods, we use dot notation like:
Foo.bar();
which would print the string: 'Foobar' is actually spelled 'Fubar'.
So it would be safe to assume that System.out.println is a static method. and print, and printf etc..Last edited by Newbie666; 01-27-2010 at 05:22 AM.
- 01-27-2010, 08:34 AM #8
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
OK.Thanks a lot.
But i had a question about args.
1)args is important for JVM?
2)args is important for compiler?
that is, what does the args do in our programs?
- 01-27-2010, 08:52 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
The entry point to your program is defined as a class containing a static method with the signature void main(String[] args). That's it. That's what it looks for.
The args are there so you can pass data into the app when it starts up, a bit like the various arguments you can give to java (-Xmx, -Xms, -cp, etc). That's all there is to it. It provides a way of the user starting the app to give the app some parameters.
- 01-27-2010, 10:04 AM #10
Member
- Join Date
- Jan 2010
- Posts
- 46
- Rep Power
- 0
As another example a number of PC Games allow you to start in Windowed mode by passing flags to the game such as -w, -win, or -window. Also playing the game with no sound -ns / -nosound or changing the graphics -1600x900, -1400x900 etc (usually by appending flags to the shortcut in program menu)
Typically the minus symbol is used as a convention to distinguish between settings(flags) and other strings.
Take this example, this program takes a input flag and output flag and the file names as strings. The input file has all it's lines sorted and the output of the sorted lines in the text is sent to the output file.
ProgramName -i Input_File.txt -o Output_file.txt
or
ProgramName -o Output_File.txt -i Input_File.txt
so the two switches are -i & -o
but the variable strings which can be any file names follow the flags.
The use of flags is not mandatory but it is encouraged, anything could be used as it is you the programmer who has to write the code to deal with what is sent to the program. However it is usually better to stick to standards.Last edited by Newbie666; 01-27-2010 at 10:09 AM.
- 01-27-2010, 09:17 PM #11
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
Hello
Thank you very much about your nice answers.
1)How do you know these? Do you get these with more practice or more search?
2)I run my program with public static void main() and I got an runtime error:It provides a way of the user starting the app to give the app some parameters.
Exeption in thread “main” java.lang.NoSuchmethodError:main
That is, my program does not provide a way for starting up the app.
OK?
- 01-28-2010, 08:01 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
It follows C in that respect, so that's where I learned about it. Java employs the same concept of a main() with arguments. It's pretty fundamental, and the Sun tutorials no doubt talk about it in the early parts when dealing with the structure of a Java program.
As for (2), yes...that's not a proper main.
- 01-28-2010, 11:58 AM #13
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
Similar Threads
-
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] Why main() in java is declared as public static void main?
By piyu.sha in forum New To JavaReplies: 5Last Post: 10-06-2008, 12:11 AM -
The different of static void,protected,and void in methods?
By Winarto in forum New To JavaReplies: 5Last Post: 01-24-2008, 11:53 PM


LinkBack URL
About LinkBacks


Bookmarks