Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int main(); C - java equivalent

I'm an University student and new to java. Last year, second semester we did some programming in C. I test a lot of stuff before truly understanding it. In C I only need to start with int main() to start coding. Java is a bit more confusing. Why, do I "need" (I'm not sure if I have to start this way) to start with

public static void main (String[] args)

if I'm not going to use the argument in my program. This:

public static void main ()

gives me an error. I don't want to read in any arguments.

I hope you understand - English is not my Home Language.

like image 876
ISJ Avatar asked Jun 13 '26 19:06

ISJ


2 Answers

It's just language definition.

The signature:

public static void main (String[] args)

Is the signature of the main method in Java, some languages have more than one main method signature. You don't need to worry if you don't use the args array, it will just stay empty.

This wiki page could clear things up a bit.

like image 95
Simeon Avatar answered Jun 16 '26 07:06

Simeon


C is quirky. If you declare a function as int main(), the arguments aren't checked. Properly, a C function that takes no arguments should be declared as int foo(void), not int foo(). The C main function is really int main(int argc, char *argv[]), but since C doesn't care whether you put the args in the prototype or not, it will accept int main(). Java is more of a stickler (in more than one way) than C and requires you to actually type out the arguments to its functions, including main.

like image 31
nmichaels Avatar answered Jun 16 '26 07:06

nmichaels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!