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 27, 2018 in Java by 93.lynn
• 1,570 points
502 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 27, 2018 by code.reaper12
• 3,450 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,050 points
798 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 8, 2018 in Java by geek.erkami
• 2,640 points
415 views
0 votes
1 answer

How to calculate method execution time in Java ?

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

answered May 24, 2018 in Java by Rishabh
• 3,540 points
109 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.o ...READ MORE

answered Aug 16 in Java by Sirajul
• 36,620 points
253 views
0 votes
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,540 points
76 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,300 points
83 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,350 points
87 views
0 votes
2 answers

How to clear the console in Java?

import java.io.IOException; public class CLS { ...READ MORE

answered Aug 27, 2018 in Java by Sushmita
• 6,880 points
6,597 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,450 points
74 views
0 votes
1 answer

Overriding private or static method in Java

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

answered Jul 30, 2018 in Java by code.reaper12
• 3,450 points
98 views