63. JOptionPane: Terminate your program completely with System.exit() - Learn Java

Sharing buttons:

okay I'm hello everyone I hope you're

all doing well okay so this was our last

program using dialog boxes to be

specific the G option pain class so

let's 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 that's showing for

dialogue method always returns okay

whatever the user types it returns it

back and so when it's 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 we'll 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 it's not done

executing it doesn't stop there but it's

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


okay I'm sorry unless an extra task is

it's basically basically it's 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 let's just run this program to

see first of all see how it runs and

I'll kind of point out how you can

visually see that task right we can't

see with our eyes but it happens behind

the scenes so I'm 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 it's it's basically the

icon for that for the dialog box when I

hit OK we can see that it's 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

it's really causing harm to your program

you know the future in future in the

future it can cause harm in some way

right it's possible so you want to

basically end your program terminated

completely and the way you do do this is

after you've after your last line of

code you basically call make a call to

system exit and system that exit will

basically terminates that thread I'm

also going to pass in a number 0 and

I'll 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

I'm passing in this zero I'm 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 it's 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 it's 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

it's 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 it's an

indication that the program run

successfully so let's test this again to

see if we're 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 it's going to hit the

system that exit and then this should go

away so I'm 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 that's that's

basically this call again this is this

is a method method exit this a method

right so we are making a call to it

we'll 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 what's called arguments into

the method in the parenthesis we'll 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 you're done using it make a

call to a system that exit to terminates

that extra thread that is started by the

Egyptian pain let's run it one more time

since I have disabled I've basically

told off to Java ok to not show me that

dialog box again that that icon will

just disappear disappear and we wouldn't

see anything else

so compile run let's try it one more

time just going to use let's just type

in the for name then last name so I'm

just going to have it here I'm going to

hit enter 2 for 4 ok right so let's see

if that works

ok I'm gonna hit enter one more time and

then now you see this icon disappear so

hit OK and it's 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 you'd create a program you may

think you've closed the program but then

it's still running which uses memory and

I'll 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 I'll do

everything to respond to no comments

down below in the comment section take

care of yourselves and I'll see you next

time with the next video alright then

bye bye