Responsive Advertisement

Java์˜ Boolean ๋ฐ์ดํ„ฐ ํƒ€์ž…: ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ํ™œ์šฉ๋ฒ•

Java์—์„œ Boolean ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ฐธ(true) ๋˜๋Š” ๊ฑฐ์ง“(false)์˜ ๋‘ ๊ฐ€์ง€ ๊ฐ’๋งŒ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์ž…๋‹ˆ๋‹ค. Boolean ํƒ€์ž…์€ ์กฐ๊ฑด๋ฌธ๊ณผ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์„ ๋‹ค๋ฃฐ ๋•Œ ํ•„์ˆ˜์ ์ด๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„ ์ œ์–ด์— ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Java์—์„œ Boolean ํƒ€์ž…์„ ์„ ์–ธํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•, ๊ทธ๋ฆฌ๊ณ  ๋‹ค์–‘ํ•œ ํ™œ์šฉ ์˜ˆ์ œ๋ฅผ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

Boolean ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋ž€?

boolean ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ true ๋˜๋Š” false ๊ฐ’๋งŒ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Boolean ๋ณ€์ˆ˜๋Š” ์ฃผ๋กœ ์กฐ๊ฑด๋ฌธ์ด๋‚˜ ๋ฐ˜๋ณต๋ฌธ์—์„œ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ์˜ ๋…ผ๋ฆฌ์  ํ๋ฆ„์„ ์ œ์–ดํ•˜๋Š” ๋ฐ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

Boolean ๋ณ€์ˆ˜ ์„ ์–ธ ์˜ˆ์ œ

public class BooleanExample {
    public static void main(String[] args) {
        boolean isJavaFun = true;
        boolean isFishTasty = false;

        System.out.println("Java๋Š” ์žฌ๋ฏธ์žˆ๋‹ค: " + isJavaFun);
        System.out.println("๋ฌผ๊ณ ๊ธฐ๋Š” ๋ง›์žˆ๋‹ค: " + isFishTasty);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

Java๋Š” ์žฌ๋ฏธ์žˆ๋‹ค: true
๋ฌผ๊ณ ๊ธฐ๋Š” ๋ง›์žˆ๋‹ค: false

์œ„ ์ฝ”๋“œ์—์„œ๋Š” isJavaFun ๋ณ€์ˆ˜์— true, isFishTasty ๋ณ€์ˆ˜์— false ๊ฐ’์„ ํ• ๋‹นํ•˜์—ฌ, ๊ฐ๊ฐ์˜ ๋…ผ๋ฆฌ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ถœ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

Boolean ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ํ™œ์šฉ

1. ์กฐ๊ฑด๋ฌธ์—์„œ Boolean ํ™œ์šฉ

Boolean ํƒ€์ž…์€ ์ฃผ๋กœ if, else์™€ ๊ฐ™์€ ์กฐ๊ฑด๋ฌธ์—์„œ ์‚ฌ์šฉ๋˜์–ด ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ

public class ConditionalExample {
    public static void main(String[] args) {
        boolean isRaining = true;

        if (isRaining) {
            System.out.println("์šฐ์‚ฐ์„ ๊ฐ€์ ธ๊ฐ€์„ธ์š”.");
        } else {
            System.out.println("๋ง‘์€ ๋‚ ์”จ์ž…๋‹ˆ๋‹ค!");
        }
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

์šฐ์‚ฐ์„ ๊ฐ€์ ธ๊ฐ€์„ธ์š”.

์œ„ ์ฝ”๋“œ์—์„œ๋Š” isRaining ๋ณ€์ˆ˜๊ฐ€ true์ผ ๋•Œ "์šฐ์‚ฐ์„ ๊ฐ€์ ธ๊ฐ€์„ธ์š”."๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. false์ผ ๊ฒฝ์šฐ์—๋Š” "๋ง‘์€ ๋‚ ์”จ์ž…๋‹ˆ๋‹ค!"๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

2. ๋ฐ˜๋ณต๋ฌธ์—์„œ Boolean ํ™œ์šฉ

boolean ๋ณ€์ˆ˜๋Š” while๊ณผ ๊ฐ™์€ ๋ฐ˜๋ณต๋ฌธ์—์„œ๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํŠน์ • ์กฐ๊ฑด์ด true์ธ ๋™์•ˆ ๋ฐ˜๋ณต ์‹คํ–‰ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ

public class WhileLoopExample {
    public static void main(String[] args) {
        boolean keepGoing = true;
        int counter = 0;

        while (keepGoing) {
            System.out.println("์นด์šดํ„ฐ: " + counter);
            counter++;
            if (counter >= 5) {
                keepGoing = false;
            }
        }
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

์นด์šดํ„ฐ: 0
์นด์šดํ„ฐ: 1
์นด์šดํ„ฐ: 2
์นด์šดํ„ฐ: 3
์นด์šดํ„ฐ: 4

์œ„ ์ฝ”๋“œ์—์„œ๋Š” keepGoing์ด true์ธ ๋™์•ˆ counter ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉฐ, counter๊ฐ€ 5 ์ด์ƒ์ด ๋˜๋ฉด keepGoing์„ false๋กœ ์„ค์ •ํ•ด ๋ฐ˜๋ณต์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

3. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์™€ Boolean

Java์—์„œ๋Š” Boolean ํƒ€์ž…๊ณผ ํ•จ๊ป˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณต์žกํ•œ ์กฐ๊ฑด์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋กœ๋Š” && (AND), || (OR), ! (NOT)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ

public class LogicalOperatorsExample {
    public static void main(String[] args) {
        boolean isWeekend = true;
        boolean isSunny = false;

        if (isWeekend && isSunny) {
            System.out.println("๋‚ ์”จ๊ฐ€ ์ข‹์œผ๋‹ˆ ์†Œํ’์„ ๊ฐ€์ž!");
        } else if (isWeekend || isSunny) {
            System.out.println("์†Œํ’์„ ๊ฐˆ์ง€ ๊ณ ๋ฏผํ•ด ๋ณด์ž.");
        } else {
            System.out.println("์˜ค๋Š˜์€ ์†Œํ’์„ ๊ฐ€์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค.");
        }
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

์†Œํ’์„ ๊ฐˆ์ง€ ๊ณ ๋ฏผํ•ด ๋ณด์ž.

์œ„ ์ฝ”๋“œ์—์„œ๋Š” isWeekend์™€ isSunny์˜ ์กฐํ•ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ถœ๋ ฅ์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. isWeekend && isSunny๋Š” ๋‘˜ ๋‹ค true์ผ ๋•Œ๋งŒ ์ฐธ์ด ๋˜๋ฉฐ, isWeekend || isSunny๋Š” ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ true์ผ ๊ฒฝ์šฐ ์ฐธ์ด ๋ฉ๋‹ˆ๋‹ค.

Boolean ๋ž˜ํผ ํด๋ž˜์Šค

Java์˜ Boolean์€ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์™ธ์—๋„ ๋ž˜ํผ ํด๋ž˜์Šค Boolean์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ํƒ€์ž…๊ณผ ๋‹ฌ๋ฆฌ Boolean ๊ฐ์ฒด๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ์ฒด๋กœ ๋‹ค๋ฃฐ ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

Boolean ๊ฐ์ฒด ์ƒ์„ฑ ์˜ˆ์ œ

public class BooleanWrapperExample {
    public static void main(String[] args) {
        Boolean isActive = Boolean.valueOf(true);
        Boolean isClosed = Boolean.parseBoolean("false");

        System.out.println("isActive: " + isActive);
        System.out.println("isClosed: " + isClosed);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

isActive: true
isClosed: false

Boolean.valueOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด true ๋˜๋Š” false ๊ฐ’์„ ๊ฐ–๋Š” Boolean ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, Boolean.parseBoolean ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด ๊ฐ’์„ Boolean์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

Boolean๊ณผ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž

Boolean ํƒ€์ž…์€ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž(condition ? value1 : value2)์™€ ํ•จ๊ป˜ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋ฉฐ, ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฐ’์„ ์„ ํƒํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

์‚ผํ•ญ ์—ฐ์‚ฐ์ž ์˜ˆ์ œ

public class TernaryExample {
    public static void main(String[] args) {
        boolean isMember = true;
        String accessLevel = isMember ? "์ ‘๊ทผ ํ—ˆ์šฉ" : "์ ‘๊ทผ ๋ถˆ๊ฐ€";

        System.out.println("์‚ฌ์šฉ์ž ์ ‘๊ทผ ์ˆ˜์ค€: " + accessLevel);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

์‚ฌ์šฉ์ž ์ ‘๊ทผ ์ˆ˜์ค€: ์ ‘๊ทผ ํ—ˆ์šฉ

์œ„ ์ฝ”๋“œ์—์„œ๋Š” isMember๊ฐ€ true์ผ ๊ฒฝ์šฐ "์ ‘๊ทผ ํ—ˆ์šฉ"์ด ํ• ๋‹น๋˜๊ณ , false์ผ ๊ฒฝ์šฐ "์ ‘๊ทผ ๋ถˆ๊ฐ€"๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

Java์˜ boolean ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์กฐ๊ฑด์„ ๋‹ค๋ฃจ๊ณ  ๋…ผ๋ฆฌ์  ํ๋ฆ„์„ ์ œ์–ดํ•˜๋Š” ๋ฐ ํ•„์ˆ˜์ ์ธ ์š”์†Œ์ž…๋‹ˆ๋‹ค. Boolean ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ™œ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ์œ ์—ฐ์„ฑ๊ณผ ํšจ์œจ์„ฑ์ด ํฌ๊ฒŒ ํ–ฅ์ƒ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ ๋‹ค๋ฃฌ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•๊ณผ ๋‹ค์–‘ํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด Java์˜ Boolean ํƒ€์ž…์„ ์ดํ•ดํ•˜๊ณ , ์‹ค์ œ ์ฝ”๋“œ์—์„œ ํšจ์œจ์ ์œผ๋กœ ํ™œ์šฉํ•ด ๋ณด์„ธ์š”!

๋Œ“๊ธ€ ์“ฐ๊ธฐ