I am taking a java class, and my teacher really hasn't been to clear on anything, and I was wondering why
public static void main (String args[])
needed to be added in excuting programs. What is the purpose in adding that many types?
void main() is the entry point of the application. This function will be called by the virtual machine when it loads your application.
public means it's accessible outside of the class you declare it in.
static means that no class instance needs to be created in order to call it; it is shared between all instances of the class it is declared in.
void is the return type (ie, no return value)
args[] is a collection of the command line tokens passed to the program.
HTH.
thank you for that explination my comp sci teacher failed to give.
what does hth mean?
Happy to help.
Quote from: hismajesty[yL] on August 27, 2004, 04:20 PM
Happy to help.
Or "Hope that helped."
depending on whether or not I was indeed, happy.
Quote from: K on August 27, 2004, 05:09 PMdepending on whether or not I was indeed, happy.
:P
Isn't
public static void main(String args[])
bad syntax? Isn't it supposed to be
public static void main(String[] args)
???
:P
Quote from: MyndFyre on August 28, 2004, 07:31 PM
Isn't
public static void main(String args[])
bad syntax? Isn't it supposed to be
public static void main(String[] args)
???
:P
They both work. In oldschool java, String []args was only valid. But, I'm told, people complained that the non-c-style syntax confused people.so they changed it.
Quote from: MyndFyre on August 28, 2004, 07:31 PM
Isn't
public static void main(String args[])
bad syntax? Isn't it supposed to be
public static void main(String[] args)
???
:P
The way it is being used makes it a matter of preference IMO. The only time a declaration of an array on the variable(s) type would seem more correct to me is if you were declaring multiple arrays of the same type as follows:
public int[] intArray1, intArray2, ... ;
This would eliminate having to declare each variable as an array directly.
As for which way you prefer to use in a method signature, I don't see how ether
public static void main (String[] args)
or
public static void main (String args[])
is more correct than the other.
IMHO, it's because the type of "args" is String[]. I guess it's purely cosmetic, but I'm of the school of thought that when declaring locals, it should be
type identifier
vs
type identifier type
:P
Quote from: MyndFyre on September 01, 2004, 06:59 PMIMHO, it's because the type of "args" is String[]. I guess it's purely cosmetic
Well, it depends whether you think of [] as modifying the type or as specifying a new type. For a screwed up language like Java, I can understand treating an array of a type as being completely different; for C, it makes a lot more sense to use the [] after the variable name, since it's a modifier on the type of the variable. You're declaring that the variable isn't just an int, but an
array of int.
Quote from: MyndFyre on September 01, 2004, 06:59 PM
IMHO, it's because the type of "args" is String[]. I guess it's purely cosmetic, but I'm of the school of thought that when declaring locals, it should be
type identifier
vs
type identifier type
:P
Yes, so then like I said it comes down to being a matter of preference.
Quote from: Tuberload on September 01, 2004, 10:38 PM
Yes, so then like I said it comes down to being a matter of preference.
You know Tuber, I must admit that I have NOT missed you ALWAYS having to get the last word in. :P
QuoteFor a screwed up language like Java, I can understand treating an array of a type as being completely different;
That's because Arrays in Java are treated like objects, whereas a basic type such as int is not
Then again, String is also an object