Bibi's DevLog ๐Ÿค“๐ŸŽ

[Java] ์ž๋ฐ” ์ƒ์ˆ˜์™€ ์ƒ์ˆ˜ ์ปจ๋ฒค์…˜ / System.exit(0) ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Java ์ž๋ฐ”

[Java] ์ž๋ฐ” ์ƒ์ˆ˜์™€ ์ƒ์ˆ˜ ์ปจ๋ฒค์…˜ / System.exit(0)

๋น„๋น„ bibi 2021. 3. 2. 23:42

๋ฆฌ๋ทฐ์–ด ๋ถ„๊ป˜์„œ ๋งˆ์ง€๋ง‰ ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋‹ฌ์•„ ์ฃผ์‹  ๋‚ด์šฉ์— ๋ฐฐ์šธ ์ ์ด ๋งŽ์•„์„œ, ๊ทธ ๋ถ€๋ถ„์„ ์ •๋ฆฌํ–ˆ๋‹ค.

์ž๋ฐ” ์ƒ์ˆ˜์™€ ์ƒ์ˆ˜ ์ปจ๋ฒค์…˜

Q.

์ž๋ฐ” ์ƒ์ˆ˜๋ช…์€ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž์— Snake Case๋ฅผ ์“ด๋‹ค, ์ง€์—ญ๋ณ€์ˆ˜๋Š” ์†Œ๋ฌธ์ž์— Camel Case๋ฅผ ์“ด๋‹ค ์ด ์ •๋„๋งŒ ์•Œ๊ณ  ์žˆ๋Š”๋ฐ, ์œ„์™€ ๊ฐ™์ด ์ง€์—ญ๋ณ€์ˆ˜์ธ ์ƒ์ˆ˜์˜ ๊ฒฝ์šฐ๋Š” ์–ด๋–ป๊ฒŒ ์ง€์–ด์•ผ ํ•˜๋‚˜์š”?๐Ÿค”
์šฐ์„ ์€ ์ƒ์ˆ˜๋‹ˆ๊นŒ ๋Œ€๋ฌธ์ž๋กœ ์“ฐ๊ธด ํ–ˆ๋Š”๋ฐ, ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ƒ์ˆ˜์ด๊ธฐ ์ „์— ์ง€์—ญ๋ณ€์ˆ˜์ด๋ฏ€๋กœ start end์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š” ๊ฑด๊ฐ€์š”?

์ฝ”๋ฉ˜ํŠธ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!๐Ÿ™‡โ€โ™€๏ธ

A.

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)e

Code Conventions for the Java Programming Language : 9 - Naming Conventions์— ๋”ฐ๋ฅด๋ฉด static final์ด ๋ถ™์€ ํด๋ž˜์Šค ์ƒ์ˆ˜๋Š” ๋Œ€๋ฌธ์ž ์–ธ๋”์Šค์ฝ”์–ด๋ฅผ ์‚ฌ์šฉํ•œ ๋„ค์ด๋ฐ ๊ทœ์น™(uppercase with words separated by underscores ("_"))์„ ๋”ฐ๋ฅธ๋‹ค๊ณ  ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค. ์ž๋ฐ”์—์„œ final์ด ๋ถ™์œผ๋ฉด ์žฌํ• ๋‹น์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋…์ƒ ์ƒ์ˆ˜์ด๊ธด ํ•˜์ง€๋งŒ, ์ผ๋ฐ˜์ ์œผ๋กœ ์ž๋ฐ” ์ƒ์ˆ˜๋ผ๊ณ  ํ•˜๋ฉด static final ๋ถ™์€ ํด๋ž˜์Šค ์ƒ์ˆ˜๋ฅผ ๋งํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค. ์ €๋„ ํด๋ž˜์Šค ์ƒ์ˆ˜์˜ ์˜๋ฏธ์—์„œ ์ž๋ฐ” ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•  ๊ฒƒ์„ ๋ง์”€๋“œ๋ ธ์Šต๋‹ˆ๋‹ค. ์ฐพ์•„๋ณธ ์ž๋ฃŒ๋‚˜ ๊ทธ๋™์•ˆ ๋ดค๋˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด static์ด ๋ถ™์ง€ ์•Š์€ ์ธ์Šคํ„ด์Šค ์ƒ์ˆ˜๋‚˜, ์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ ์ง€์—ญ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์นด๋ฉœ ์ผ€์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์€๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. (๋ฌผ๋ก  ์†ํ•œ ํŒ€์˜ ์ปจ๋ฒค์…˜์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

ํ˜„์žฌ ์ฝ”๋“œ์—์„œ START,END๋Š” ํด๋ž˜์Šค ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•˜๋Š”๊ฒŒ ๋” ๋‚˜์•„๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” "start", "end"๋ผ๋Š” ๋ฌธ์ž์—ด์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋˜์—ˆ๋‹ค๊ฐ€ ๋๋‚ ๋•Œ ํ•ด์ œ๋  ํ•„์š”๊ฐ€ ์—†๊ณ , ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ์ดˆ๊ธฐํ™”๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ํ•œ๋ฒˆ static ์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•œ๋’ค ๋ฉ”์„œ๋“œ๋‚˜ ๊ฐ์ฒด์˜ ํ˜ธ์ถœ ํšŸ์ˆ˜์— ์ƒ๊ด€์—†์ด ์žฌ์‚ฌ์šฉ ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์ •๋ฆฌํ•˜์ž๋ฉด,

  • ์ž๋ฐ”์—์„œ final์„ ๋ถ™์—ฌ ๋งŒ๋“  ๋ณ€์ˆ˜(์ƒ์ˆ˜)๋Š” ํด๋ž˜์Šค ์ƒ์ˆ˜์— ํ•œํ•ด ๋Œ€๋ฌธ์ž ์–ธ๋”์Šค์ฝ”์–ด๋ฅผ ์‚ฌ์šฉํ•œ ๋„ค์ด๋ฐ์„ ํ•˜๊ณ , ๋‚˜๋จธ์ง€๋Š” ์นด๋ฉœ ์ผ€์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์œ„ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ๋Š” ํด๋ž˜์Šค ์ƒ์ˆ˜๋กœ ์„ ์–ธํ•˜๋Š”๊ฒŒ ๋‚ซ๋‹ค.

๊ด€๋ จ ๋‚ด์šฉ์€ ๋งํฌ๋ฅผ ์ฒจ๋ถ€ํ• ํ…Œ๋‹ˆ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ๊ณ ๋ฏผํ•ด์ฃผ์„ธ์š”๐Ÿ˜Š


System.exit(0)

Q

System.exit(0);์ด ๊ผญ ํ•„์š”ํ• ๊นŒ์š”?

์—†์–ด๋„ ์ž˜ ์ข…๋ฃŒ๋˜๊ธด ํ•˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋œ๋‹ค๋Š” ๊ฑธ ๋ช…์‹œ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์–ด์„œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ์ข‹์€ ๋ฉ”์†Œ๋“œ์ธ๊ฐ€์š”?๐Ÿ˜ฎ (์šฐ์„  ์‚ญ์ œํ–ˆ์Šต๋‹ˆ๋‹ค!)

A

System.exit(0);๋Š” ๋ฉ”์ธ ๋ฉ”์„œ๋“œ๋ฅผ ์ข…๋ฃŒํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๊ฐ€ ์—†์–ด๋„ ๋ฉ”์ธ ๋ฉ”์„œ๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๋  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ํ•„์š”์—†๋Š” ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. System.exit(0)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.

  1. ํŠน์ • ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
  2. ๋ฉ”์ธ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹Œ ๊ณณ์—์„œ ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๊ณ  ์‹ถ์„๋•Œ

The typical use-case for System.exit is when there is an abnormal condition and we need to exit the program immediately.

Also, if we have to terminate the program from a place other than the main method, System.exit is one way of achieving it.

์•„๋ž˜ ๋งํฌ ๊ธ€๋„ ํ•œ ๋ฒˆ ์ฝ์–ด๋ณด์„ธ์š”~