System exit method in Java

0 votes

How does System.exit() method affect a program?

public class TestExit
{      
    public static void main(String[] args)
    { 
        System.out.println("hello world");

        System.exit(0);
    }      
}

Is it necessary to include this method in my code? Please explain.

Sep 28, 2018 in Java by 93.lynn
• 1,600 points
1,535 views

1 answer to this question.

0 votes

System.exit() is a method of System class and is used to execute the shutdown hooks before the program quits. This method is generally used to shut down the larger programs where all parts of the program are not aware of each other. In such cases, System.exit() is called in order to take care of necessary shutdown tasks like closing file, closing connection, freeing resources etc.

One thing I would like to mention, this method never returns normally i.e the method won't return anything, once a thread goes there, it won't come back.

Also, if you have code which contains the non-daemon threads, you need to use System.exit() to shut down all non-daemon threads and release other resources. If there are no other non-daemon threads, returning from main will automatically shut down the JVM and will call the shutdown hooks.

answered Sep 28, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,129 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 9, 2018 in Java by geek.erkami
• 2,680 points
2,115 views
0 votes
1 answer

How to calculate method execution time in Java ?

Use the following code : new Timer(""){{ ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
1,704 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,230 points
2,415 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,344 views
0 votes
1 answer

What are optional parameters in Java

Using three dots: public void move(Object... x) { ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,350 points
1,008 views
0 votes
1 answer

How to split Strings by space in Java ?

You can use split() method. str = "Hello ...READ MORE

answered May 16, 2018 in Java by sharth
• 3,370 points
1,614 views
0 votes
3 answers

How to clear the console in Java?

import java.io.IOException; public class chkClearScreen {     public static void ...READ MORE

answered Aug 26, 2020 in Java by ItsJustRaja
36,153 views
0 votes
1 answer

Overloaded method for null in Java

The method invoked here will be the ...READ MORE

answered May 23, 2018 in Java by code.reaper12
• 3,500 points
917 views
0 votes
1 answer

Overriding private or static method in Java

You cannot override a private or static ...READ MORE

answered Jul 31, 2018 in Java by code.reaper12
• 3,500 points
16,531 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP