how to end a program in java This is a topic that many people are looking for. militaria-agent.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, militaria-agent.com would like to introduce to you 63. JOptionPane: Terminate your program completely with System.exit() – Learn Java. Following along are instructions in the video below:
Im hello. Everyone i hope youre all doing well okay. So this was was our last program using dialog boxes to be specific the g option.
Pain class. Lets just quickly go through it to see what it does so basically we imported. The german pain class over here and we called the g ocean pain.
That show input dialog to kind of display a dialog box to a user with a message on there telling the user to enter their first name it also had a text box so the user could enter their first name so the user enters their first name and stores. The first name we know that g option pain the option pain thats showing for dialogue method always returns okay whatever the user types it returns it back and so when its returned back we stir it in user first name. We did the same thing for user last name.
So we basically after the user types in your first and last name. We will have those names stored in these two variables. And then we use the g option painted show message dialog.
Which is basically another dialog. Another dialog box to show what user typed and now over here basically sent us the dialog box on the screen. We if we have a program that has multiple dialog boxes.
We can change this value here to basically offsets the position. But now well basically center the dialog box. And it will display whatever you you type into this show message dialog as a second argument.
All right so all like this kind of class anytime. You use the geo champagne class. Even if the program.
Okay executes. The last line of your code. Its not done executing it doesnt stop there.
But its kind of with this kind of you know class. And you if you write the program using this kind of class once the program executes. The last line.
Yeah once the last line is executed the program is done. But with the geo champagne anytime you use the ocean pain class. An extra thread okay.
Im sorry unless. An extra task is its basically basically its basically run okay. If you cause an extra task to run and that task is called a threat basically so the thing is you have to you have to basically do one more one more step to close that or to end that task anytime you use your campaign.
That task gets run. Which is called a threat and we have to basically do one more thing. Oh basically go one more step where step further to terminate that task.
So lets just run this program to see first of all see how it runs and ill kind of point out how you can visually. See that task right we cant see with our eyes. But it happens behind the scenes.
So im going to enter a first name and then hit enter and enter last name and you can see over here this kind of icon right. Its its basically the icon for that for the dialog box. When i hit ok we can see that its working.
It tells the tale. It says hello my name and then good morning and we can see this over here. Once i hit ok.
The program is done the last line of the code has been executed. But we can see that this like this thing is still here this icon is still here when i click it nothing happens so visually you can kind of you can kind of visually view that that task or that or that thread by you know the icon over here in windows. It may be a bit different.
It michael looks like this so. The reason. Why this is still running is because you know you can you can kind of visually view it that way that oh that extra task that is started by the jefferson pain is still running and so we have to terminate it in order to really stop this program.
Otherwise it may still be running and you know it can give you although it mean you may not see that its really causing harm to your program. You know the future in future in the future. It can cause harm in some way right.
Its possible so you want to basically end your program terminated completely and the way you do do this is after youve after your last line of code. You basically call make a call to system exit and system that exit will basically terminates that thread. Im also going to pass in a number 0.
And ill explain that in a second so you call system that exit. Which is going to basically terminate that extra task. Which is called a thread.
Which is that is started by the eruption pain and im passing in this zero. Im passing in zero over here and this zero is basically passed back to the operating system. And when the present system sees that zero was passed its normally an indication that your program runs successfully it got to this point and zero was passed back or zero.
Was sent to the operating system operating system is able to say okay your program run successfully because it was able to run all these lines and get to this point get to this last line and zero. You know zero is able to be passed back to the producer. Which is normally an indication that the program runs successfully system that exit would tell me that extra thread also now in the future.
We can test on this value you can test this value we can work with this zero value you know to be to basically check. We can check to see okay what zero pass to the operating system and if its if it if it was then we we kind of have an idea that a program run successfully so we can write code to kind of do something if the program runs successfully all right. Its just an idea.
But just know that system that system that exit determinates that extra extra task that is started from the g option pain and zero is an indication zeros passed back to the operating system. Its an indication that the program run successfully so lets test this again to see if were going to see this icon. Once the program is done running so compile this again run it going to enter my name and then last name first name and last name okay okay.
But you can see still there. But once i hit ok. The program is done running right.
Its going to hit the system that exit and then this should go away. So im going to hit ok and we can see that it went away and if you saw it. But it happens to happen very.
Quickly and also dr. Java is telling. Me that the interaction window was terminated by a called system that exit.
So thats thats basically this call. Again. This is this is a method method exit.
This a method right so we are making a call to it well talk more about methods. We are making a call to this method. When we just use a name we are call and when we type in a value here.
We have passing in whats called arguments into the method in the parenthesis. Well talk more about methods. So saying the interactions window was terminated by a culture system.
But exit and interactions window will now be restarted right. So you can you can basically say do not show this message. Again i tend to do that and hit ok.
And we can see that that icon is not no longer there so that extra task. Which is called a thread has been terminated any time users eruption pane. Once youre done using it make a call to a system that exit to terminates that extra thread that is started by the egyptian pain lets run it one more time since i have disabled.
Ive basically told off to java ok to not show me that dialog box again that that icon will just disappear disappear. And we wouldnt see anything else so compile run lets try it one more time just going to use lets just type in the for name then last name. So im just going to have it here.
Im going to hit enter. 2 for 4 ok right so lets see if that works ok im gonna hit enter one more time and then now you see this icon disappear so hit ok and its gone and so you can view you you can you can kind of visualize that thread as that icon you saw in both windows on both windows and mac. But really the idea is that an extra task.
Which is called a thread is started any time you use the g option pane class. And so you have to terminate it right if not you may want you know maybe problems with memories or memory or something in the future right. But your thing is you we want to terminate the program have the program stop running right because otherwise youd create a program you may think youve closed the program.
But then its still running which uses memory and ill call kinds of stuff right potential problems in the future. You just want to get rid of it ok. So.
If you have any questions. So just make sure you call make a call to system that exits right all right. If you have any questions.
Please let me know and ill do everything to respond to no comments down below in the comment section. Take care of yourselves and ill see you next time with the next video alright then bye bye. .
Thank you for watching all the articles on the topic 63. JOptionPane: Terminate your program completely with System.exit() – Learn Java. All shares of militaria-agent.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.