Java์ System ํด๋์ค์๋ SystemOne์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ํ๋์ ๋ฉ์๋๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. ๊ทธ ์ค ํ์ฌ ์คํ ์ค์ธ ํ๋ก๊ทธ๋จ์ด๋ JVM ์ธ์คํด์ค๋ฅผ ์ข
๋ฃํด์ผ ํ ๋ Java ์ธ์ด์์ ์ฌ์ฉ๋๋ System.exit() ๋ฉ์๋๊ฐ ์์ต๋๋ค. exit ๋ฉ์๋ ์๋์ ์ฝ์
๋ ์ค์ ๋๋ฌํ ์ ์์ผ๋ฉฐ ์คํ๋์ง ์์ต๋๋ค.
System.exit() ๋ฉ์๋์ ๊ตฌ๋ฌธ
System.exit() ๋ฉ์๋ ์ ๊ตฌ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
public void static(int status)
๋ฐ๋ผ์ ์ ์ ๋ฉ์๋์์ ์ ์ ์์ต๋๋ค. ์ค์ ๋ก System ํด๋์ค ์ ๋ชจ๋ ๋ฉ์๋๋ ์ ์ ๋ฉ์๋์
๋๋ค. exit () ๋ฉ์๋๋ ์ธ์๋ก ์ ์๋ฅผ ์ฌ์ฉํ๊ณ ์๋ฌด ๊ฒ๋ ๋ฐํํ์ง ์์ต๋๋ค. ๋ฐ๋ผ์ ์ข
๋ฃ ๋ฐฉ๋ฒ์ System.exit(i) ๋ก ํธ์ถํฉ๋๋ค. ์ฌ๊ธฐ์ i๋ ์ ์์
๋๋ค. ์ด ์ ์๋ฅผ "์ข
๋ฃ ์ํ"๋ผ๊ณ ํ๋ฉฐ 0์ด๊ฑฐ๋ 0์ด ์๋ ์์์ผ ์ ์์ต๋๋ค. ์ํ๊ฐ 0์ด๋ฉด โ exit(0) ํ๋ก๊ทธ๋จ์ด ์ฑ๊ณต์ ์ผ๋ก ์ข
๋ฃ๋ฉ๋๋ค. 0์ด ์๋ ์ํ - exit(1)๋ JVM์ ๋น์ ์์ ์ธ ์ข
๋ฃ๋ฅผ ๋ํ๋
๋๋ค.
System.exit() ๋ฉ์๋์ ์
์ํ๊ฐ 0 ๋ฐ 0์ด ์๋ ์ ์์ธ exit() ๋ฉ์๋ ์ ๊ฐ๋จํ ๋ ๊ฐ์ง ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค . ์ฒซ ๋ฒ์งธ ์์๋ ์์ ๋ฐฐ์ด์ ๋ํ ๋ฃจํ๊ฐ ์์ต๋๋ค. ๋ฃจํ๊ฐ "๋ น์"์ ๋ง๋๋ฉด ์ ํ๋ฆฌ์ผ์ด์ ์ ์ข ๋ฃํด์ผ ํฉ๋๋ค.
import java.lang.*;
class Main {
public static void main(String[] args) {
String colors[]= {"red","blue","green","black","orange"};
for(int i=0;i<colors.length;i++) {
System.out.println("Color is "+colors[i]);
if(colors[i].equals("green")) {
System.out.println("JVM will be terminated after this line");
System.exit(0);
}
}
}
}
๋ค์ ์ถ๋ ฅ์ด ํ์๋ฉ๋๋ค. ํฐ๋ฏธ๋์ ์ํ๋ก 0์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์ถ๋ ฅ์ ์ข
๋ฃ ์ฝ๋๋ฅผ ํ์ํ์ง ์์์ต๋๋ค. 0์ ์ฑ๊ณต์ ์ธ ์ข
๋ฃ๋ฅผ ๋ํ๋ด๋ฏ๋ก ์ข
๋ฃ ์ฝ๋๋ฅผ ์ธ์ํ ํ์๊ฐ ์์ต๋๋ค. ๋ค์ ์์ ์์๋ ์์ ์ ์๋ฅผ ์ํ๋ก ์ฌ์ฉํ๊ฒ ์ต๋๋ค. ์ด ์์ ์์๋ 0๊ณผ 10 ์ฌ์ด์ ์์์ ์ซ์๋ฅผ ์์ฑํ๋ ๋ฃจํ๋ฅผ ๋ง๋ญ๋๋ค. ์์ฑ๋ ์ซ์๊ฐ 2,3 ๋๋ 7์ธ ๊ฒฝ์ฐ ์์ฉ ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํด์ผ ํ๋ฉฐ ์ข
๋ฃ๋ฅผ ์ ๋ฐํ๋ ์ซ์๋ฅผ ์ธ์ํด์ผ ํฉ๋๋ค. ์๋ ์ฝ๋๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
import java.lang.*;
import java.util.Random;
class Main {
public static void main(String[] args) {
System.out.println("program will be terminated when values are 2, 3, or 7");
int i;
Random number=new Random();
while(true){
i = number.nextInt(11);
System.out.println("Random Number is "+i);
if(i==2||i==3||i==7){
System.out.println("Value is "+ i + " your program will be terminated now");
System.exit(i);
}
}
}
}
์ฝ๋๋ฅผ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋ํ๋ฉ๋๋ค. ๋ณด์๋ค์ํผ 3 ๋ฒ์ ์์ฉ ํ๋ก๊ทธ๋จ์ ๋น์ ์ ์ข
๋ฃ๋ฅผ ์ผ์ผ์ผฐ์ต๋๋ค. ์ด์ ์ํ ์ฝ๋๋ฅผ ํจ๊ณผ์ ์ผ๋ก ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
GO TO FULL VERSION