+ 1

why is the main in java "void" while in C is"int?

31st Jan 2020, 12:09 PM
Dan
Dan - avatar
17 Réponses
+ 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).
31st Jan 2020, 12:24 PM
Schindlabua
Schindlabua - avatar
+ 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.
31st Jan 2020, 12:32 PM
Schindlabua
Schindlabua - avatar
+ 5
void means that the method has no return type.
31st Jan 2020, 12:43 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
in C, is the exit return type your talking about. is the "return 0;" the one?
31st Jan 2020, 12:29 PM
Dan
Dan - avatar
31st Jan 2020, 12:47 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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.
31st Jan 2020, 1:14 PM
Schindlabua
Schindlabua - avatar
+ 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!
31st Jan 2020, 2:11 PM
Schindlabua
Schindlabua - avatar
+ 3
The short answer is that's just how they designed the two languages.
1st Feb 2020, 9:57 AM
Sonic
Sonic - avatar
+ 2
cool...thank you sir
31st Jan 2020, 12:35 PM
Dan
Dan - avatar
+ 2
Doesn't matter
31st Jan 2020, 2:52 PM
Huzaifa Khilawala
Huzaifa Khilawala - avatar
+ 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);
1st Feb 2020, 8:20 PM
Oussama Messaoudi
Oussama Messaoudi - avatar
+ 1
In Java, "void" means the method doesn't return no one value. I dunno what "int" in C means.
1st Feb 2020, 11:00 PM
Eduardo Cavalcante
Eduardo Cavalcante - avatar
+ 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.
2nd Feb 2020, 5:35 AM
Abhishek Kumar
Abhishek Kumar - avatar