+ 1
why is the main in java "void" while in C is"int?
17 odpowiedzi
+ 11
The integer return value in C is called the "exit code" and can be read by the operating system, or other programs.
I'm not sure why Java chose `void`, but in Java there is `System.exit(int)` which does the same (it exits the program and returns an integer back to the operating system).
+ 5
Yes exactly. You should `return 0` when the program ran correctly, and another number when an error happened. (Each program uses different error codes for different errors)
It's useful for when other programs run your program and they want to check if everything went ok.
+ 5
void means that the method has no return type.
+ 4
in C, is the exit return type your talking about. is the "return 0;" the one?
+ 4
~ swim ~ Yeah maybe. I was thinking it's some language design reason, it's abstracting away details about program execution that the programmer doesn't need to know about in 99% of cases. So they offload exit codes to some function.
+ 3
~ swim ~ I rewrote my comment about a hundred times going back and forth, but if any thread can halt the jvm in java then main being void makes more sense yeah.
In C a forked process can't terminate it's parent so control will always go back to main (unless you `exit()` early)
Checks out!
+ 3
The short answer is that's just how they designed the two languages.
+ 2
cool...thank you sir
+ 2
Doesn't matter
+ 1
it’s related to the higher programming interface that java propose
it hides the unrelated staff like the int that main can return.
and u can do it in java using
System.exit(Entier);
+ 1
In Java, "void" means the method doesn't return no one value. I dunno what "int" in C means.
+ 1
I think main part in c can be also be of void type because its only mean is that there is no any return type and for this instead of using return 0 we use getch().
In not sure much about java but as from name void in main its mean no return type.