+ 2

My question is why void is used with main in public static void main(String ab[])?

21st Jun 2017, 2:59 PM
Shubham Agarwal
Shubham Agarwal - avatar
4 odpowiedzi
+ 7
void means empty Here void is return type In java,main function doesn't return anything Hence we use void with main @Shubham Agarwal
21st Jun 2017, 3:06 PM
Sri Lakshmi
Sri Lakshmi - avatar
+ 3
I assume you're asking why it's void instead of int like in C++. The main may not be the last method run due to the introduction of multi-threading. This would defeat the purpose of returning something with main. I'm not sure if threads in C++ are force stopped before the main or not, but I'm pretty sure that's why Java devs decided to stick with void and not return anything rather than int.
21st Jun 2017, 3:08 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Since it is the main loop of the program, it doesn't have a return value, so it's void. When the main loop is done, the program simply ends.
21st Jun 2017, 3:05 PM
AgentSmith
0
thnxx I also understand
22nd Jun 2017, 1:23 PM
sush