Valhalla Legends Forums Archive | Java Programming | meh?

AuthorMessageTime
Forged
I am taking a java class, and my teacher really hasn't been to clear on anything, and I was wondering why
[code]public static void main (String args[])[/code]

needed to be added in excuting programs. What is the purpose in adding that many types?
August 27, 2004, 2:35 AM
K
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.
August 27, 2004, 3:14 AM
Forged
thank you for that explination my comp sci teacher failed to give.

what does hth mean?
August 27, 2004, 8:43 PM
hismajesty
Happy to help.
August 27, 2004, 9:20 PM
K
[quote author=hismajesty[yL] link=board=34;threadid=8395;start=0#msg77675 date=1093641649]
Happy to help.
[/quote]

Or "Hope that helped."

depending on whether or not I was indeed, happy.
August 27, 2004, 10:09 PM
hismajesty
[quote author=K link=board=34;threadid=8395;start=0#msg77684 date=1093644554]depending on whether or not I was indeed, happy.
[/quote]

:P
August 28, 2004, 1:28 PM
Myndfyr
Isn't

[code]
public static void main(String args[])
[/code]
bad syntax? Isn't it supposed to be
[code]
public static void main(String[] args)
[/code]

???

:P
August 29, 2004, 12:31 AM
iago
[quote author=MyndFyre link=board=34;threadid=8395;start=0#msg77890 date=1093739467]
Isn't

[code]
public static void main(String args[])
[/code]
bad syntax? Isn't it supposed to be
[code]
public static void main(String[] args)
[/code]

???

:P
[/quote]

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.
August 29, 2004, 2:38 AM
Tuberload
[quote author=MyndFyre link=board=34;threadid=8395;start=0#msg77890 date=1093739467]
Isn't

[code]
public static void main(String args[])
[/code]
bad syntax? Isn't it supposed to be
[code]
public static void main(String[] args)
[/code]

???

:P
[/quote]

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:

[code]public int[] intArray1, intArray2, ... ;[/code]

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

[code]public static void main (String[] args)[/code]
or
[code]public static void main (String args[])[/code]

is more correct than the other.
September 1, 2004, 9:39 PM
Myndfyr
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
September 1, 2004, 11:59 PM
Kp
[quote author=MyndFyre link=board=34;threadid=8395;start=0#msg78611 date=1094083140]IMHO, it's because the type of "args" is String[]. I guess it's purely cosmetic[/quote]

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.
September 2, 2004, 2:18 AM
Tuberload
[quote author=MyndFyre link=board=34;threadid=8395;start=0#msg78611 date=1094083140]
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]

Yes, so then like I said it comes down to being a matter of preference.
September 2, 2004, 3:38 AM
Myndfyr
[quote author=Tuberload link=board=34;threadid=8395;start=0#msg78670 date=1094096315]
Yes, so then like I said it comes down to being a matter of preference.
[/quote]

You know Tuber, I must admit that I have NOT missed you ALWAYS having to get the last word in. :P
September 2, 2004, 4:03 AM
TangoFour
[quote] For a screwed up language like Java, I can understand treating an array of a type as being completely different;[/quote]

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
September 2, 2004, 11:34 PM

Search