Responsive Advertisement

Java ์—ฐ์‚ฐ์ž(Operators): ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ํ™œ์šฉ๋ฒ•

Java์—์„œ ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜์™€ ๊ฐ’์„ ์‚ฌ์šฉํ•ด ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์—ฐ์‚ฐ์ž๊ฐ€ ์ œ๊ณต๋˜๋ฉฐ, ๊ฐ๊ฐ์˜ ์—ฐ์‚ฐ์ž๋Š” ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Java์˜ ์ฃผ์š” ์—ฐ์‚ฐ์ž์™€ ๊ทธ ํ™œ์šฉ๋ฒ•์„ ์˜ˆ์ œ์™€ ํ•จ๊ป˜ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

Java ์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜

Java์—์„œ ์ œ๊ณตํ•˜๋Š” ์—ฐ์‚ฐ์ž๋Š” ๊ธฐ๋Šฅ์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ถ„๋ฅ˜๋ฉ๋‹ˆ๋‹ค:

  • ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž
  • ํ• ๋‹น ์—ฐ์‚ฐ์ž
  • ๋น„๊ต ์—ฐ์‚ฐ์ž
  • ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž
  • ๋น„ํŠธ ์—ฐ์‚ฐ์ž
  • ๊ธฐํƒ€ ์—ฐ์‚ฐ์ž

1. ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž (Arithmetic Operators)

์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž๋Š” ์ˆซ์ž ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์—ฐ์‚ฐ์ž ์„ค๋ช… ์˜ˆ์ œ
+ ๋ง์…ˆ a + b
- ๋บ„์…ˆ a - b
* ๊ณฑ์…ˆ a * b
/ ๋‚˜๋ˆ—์…ˆ a / b
% ๋‚˜๋จธ์ง€ a % b

์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž ์˜ˆ์ œ

public class ArithmeticExample {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;

        System.out.println("๋ง์…ˆ: " + (a + b));
        System.out.println("๋บ„์…ˆ: " + (a - b));
        System.out.println("๊ณฑ์…ˆ: " + (a * b));
        System.out.println("๋‚˜๋ˆ—์…ˆ: " + (a / b));
        System.out.println("๋‚˜๋จธ์ง€: " + (a % b));
    }
}

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

๋ง์…ˆ: 13
๋บ„์…ˆ: 7
๊ณฑ์…ˆ: 30
๋‚˜๋ˆ—์…ˆ: 3
๋‚˜๋จธ์ง€: 1

2. ํ• ๋‹น ์—ฐ์‚ฐ์ž (Assignment Operators)

ํ• ๋‹น ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์—ฐ์‚ฐ์ž ์„ค๋ช… ์˜ˆ์ œ
= ๊ฐ’ ํ• ๋‹น a = 10
+= ๋ง์…ˆ ํ›„ ํ• ๋‹น a += 5 (a = a + 5)
-= ๋บ„์…ˆ ํ›„ ํ• ๋‹น a -= 3 (a = a - 3)
*= ๊ณฑ์…ˆ ํ›„ ํ• ๋‹น a *= 2 (a = a * 2)
/= ๋‚˜๋ˆ—์…ˆ ํ›„ ํ• ๋‹น a /= 2 (a = a / 2)
%= ๋‚˜๋จธ์ง€ ํ›„ ํ• ๋‹น a %= 2 (a = a % 2)

ํ• ๋‹น ์—ฐ์‚ฐ์ž ์˜ˆ์ œ

public class AssignmentExample {
    public static void main(String[] args) {
        int a = 10;
        a += 5; // a = a + 5
        System.out.println("a += 5: " + a);
        
        a -= 3; // a = a - 3
        System.out.println("a -= 3: " + a);

        a *= 2; // a = a * 2
        System.out.println("a *= 2: " + a);

        a /= 2; // a = a / 2
        System.out.println("a /= 2: " + a);

        a %= 3; // a = a % 3
        System.out.println("a %= 3: " + a);
    }
}

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

a += 5: 15
a -= 3: 12
a *= 2: 24
a /= 2: 12
a %= 3: 0

3. ๋น„๊ต ์—ฐ์‚ฐ์ž (Comparison Operators)

๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ๋‘ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ true ๋˜๋Š” false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์—ฐ์‚ฐ์ž ์„ค๋ช… ์˜ˆ์ œ
== ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ต a == b
!= ๊ฐ’์ด ๋‹ค๋ฅธ์ง€ ๋น„๊ต a != b
> ์™ผ์ชฝ์ด ํฐ์ง€ ๋น„๊ต a > b
< ์™ผ์ชฝ์ด ์ž‘์€์ง€ ๋น„๊ต a < b
>= ์™ผ์ชฝ์ด ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€์ง€ ๋น„๊ต a >= b
<= ์™ผ์ชฝ์ด ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€์ง€ ๋น„๊ต a <= b

๋น„๊ต ์—ฐ์‚ฐ์ž ์˜ˆ์ œ

public class ComparisonExample {
    public static void main(String[] args) {
        int a = 10, b = 20;

        System.out.println("a == b: " + (a == b));
        System.out.println("a != b: " + (a != b));
        System.out.println("a > b: " + (a > b));
        System.out.println("a < b: " + (a < b));
        System.out.println("a >= b: " + (a >= b));
        System.out.println("a <= b: " + (a <= b));
    }
}

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

a == b: false
a != b: true
a > b: false
a < b: true
a >= b: false
a <= b: true

4. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž (Logical Operators)

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ์กฐ๊ฑด์„ ๊ฒฐํ•ฉํ•˜์—ฌ ์ฐธ๊ณผ ๊ฑฐ์ง“์„ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.

์—ฐ์‚ฐ์ž ์„ค๋ช… ์˜ˆ์ œ
&& ๋…ผ๋ฆฌ AND a && b
|| ๋…ผ๋ฆฌ OR a || b
! ๋…ผ๋ฆฌ NOT !a

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž ์˜ˆ์ œ

public class LogicalExample {
    public static void main(String[] args) {
        boolean x = true, y = false;

        System.out.println("x && y: " + (x && y));
        System.out.println("x || y: " + (x || y));
        System.out.println("!x: " + (!x));
    }
}

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

x && y: false
x || y: true
!x: false

๊ฒฐ๋ก 

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

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