No! You do not always need to call System.exit(0)
to end a java program. If there is no non-daemon thread spawned by your code, the application will terminate automatically upon finishing your main thread task.
If your main method results in spawning some non-daemon thread which are still alive doing some processing while your main method has reached the end, then the application will not be terminated until those threads complete. In this case, if you explicitly call System.exit(0)
, then application will terminate immediately killing all your threads.
Please refer javadoc of Thread which mentions the details.