๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Language/Java

[Java]์ž๋ฐ” ๋Œ€์ž… ์—ฐ์‚ฐ์ž์˜ ๋ชจ๋“  ๊ฒƒ: ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‘์šฉ๊นŒ์ง€ ์•Œ์•„๋ณด๊ธฐ

by YJ Dev 2024. 4. 30.
728x90
๋ฐ˜์‘ํ˜•
SMALL

์ž๋ฐ” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๋ฐ์— ์‚ฌ์šฉ๋˜๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ์— ์žˆ์–ด์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธ€์—์„œ๋Š” ์ž๋ฐ” ๋Œ€์ž… ์—ฐ์‚ฐ์ž์˜ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‘์šฉ๊นŒ์ง€ ๋‹ค์–‘ํ•œ ์ธก๋ฉด์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ” ๋Œ€์ž…์—ฐ์‚ฐ์ž


1. ๋Œ€์ž… ์—ฐ์‚ฐ์ž๐Ÿ’Œ

๋Œ€์ž… ์—ฐ์‚ฐ์ž๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ '=' ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๋ฐ์— ์“ฐ์ž…๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜ ์ฝ”๋“œ์—์„œ 'x' ๋ณ€์ˆ˜์— ๊ฐ’ 10์„ ํ• ๋‹นํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

int x = 10;

2. ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๐Ÿ’ž

๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋Š” ๋Œ€์ž… ์—ฐ์‚ฐ์ž์™€ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž๋ฅผ ๊ฒฐํ•ฉํ•œ ๊ฒƒ์œผ๋กœ, '+='๋‚˜ '-='์™€ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์—ฐ์‚ฐ๊ณผ ๋Œ€์ž…์„ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๊ตฌ๋ถ„ ์„ค๋ช… ์˜ˆ์‹œ ๋™์ผํ•œ ํ‘œํ˜„์‹
+= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋”ํ•จ x += 5; x = x + 5;
-= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์—์„œ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋บŒ y -= 3; y = y - 3;
*= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž์— ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๊ณฑํ•จ z *= 2; z = z * 2;
/= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋กœ ๋‚˜๋ˆ” a /= 4; a = a / 4;
%= ์™ผ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์˜ค๋ฅธ์ชฝ ํ”ผ์—ฐ์‚ฐ์ž๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ b %= 7; b = b % 7;
&= ๋น„ํŠธ AND ์—ฐ์‚ฐ ํ›„ ๋Œ€์ž… c &= 2; c = c & 2;
|= ๋น„ํŠธ OR ์—ฐ์‚ฐ ํ›„ ๋Œ€์ž… d |= 3; d = d | 3;
^= ๋น„ํŠธ XOR ์—ฐ์‚ฐ ํ›„ ๋Œ€์ž… e ^= 4; e = e ^ 4;
<<= ์™ผ์ชฝ ์‹œํ”„ํŠธ ์—ฐ์‚ฐ ํ›„ ๋Œ€์ž… f <<= 2; f = f << 2;
>>= ์˜ค๋ฅธ์ชฝ ์‹œํ”„ํŠธ ์—ฐ์‚ฐ ํ›„ ๋Œ€์ž… g >>= 3; g = g >> 3;
>>>= ์˜ค๋ฅธ์ชฝ ์‹œํ”„ํŠธ (๋ถ€ํ˜ธ ์—†์Œ) ์—ฐ์‚ฐ ํ›„ ๋Œ€์ž… h >>>= 1; h = h >>> 1;


๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋Š” ๊ธฐ์กด์˜ ๋ณ€์ˆ˜ ๊ฐ’์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

public class Main {
    public static void main(String[] args) {
        // ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
        int result = 0;

        // result์— 10์„ ๋”ํ•˜์—ฌ result ๊ฐ’ ๊ฐฑ์‹ 
        result += 10;
        System.out.println("result=" + result); // ์ถœ๋ ฅ: result=10

        // result์—์„œ 5๋ฅผ ๋นผ์„œ result ๊ฐ’ ๊ฐฑ์‹ 
        result -= 5;
        System.out.println("result=" + result); // ์ถœ๋ ฅ: result=5

        // result์— 3์„ ๊ณฑํ•˜์—ฌ result ๊ฐ’ ๊ฐฑ์‹ 
        result *= 3;
        System.out.println("result=" + result); // ์ถœ๋ ฅ: result=15

        // result๋ฅผ 5๋กœ ๋‚˜๋ˆ„์–ด result ๊ฐ’ ๊ฐฑ์‹ 
        result /= 5;
        System.out.println("result=" + result); // ์ถœ๋ ฅ: result=3

        // result๋ฅผ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ result ๊ฐ’์œผ๋กœ ์„ค์ •
        result %= 3;
        System.out.println("result=" + result); // ์ถœ๋ ฅ: result=0
    }
}

3.๋Œ€์ž… ์—ฐ์‚ฐ์ž์˜ ์‘์šฉ๐Ÿ’Ÿ

์ฆ๊ฐ ์—ฐ์‚ฐ์ž์™€ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ณ€์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ฑฐ๋‚˜ ๊ฐ์†Œ์‹œํ‚ค๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ํ•  ๋•Œ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด:

public class Main {
    public static void main(String[] args) {
        // ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”
        int num1 = 5;
        int num2 = 10;

        // ๋ณ€์ˆ˜ num1์„ 1 ์ฆ๊ฐ€์‹œํ‚ด
        num1++;
        System.out.println("num1 after increment: " + num1); // ์ถœ๋ ฅ: num1 after increment: 6

        // ๋ณ€์ˆ˜ num2๋ฅผ 1 ๊ฐ์†Œ์‹œํ‚ด
        num2--;
        System.out.println("num2 after decrement: " + num2); // ์ถœ๋ ฅ: num2 after decrement: 9

        // ๋ณ€์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ ์ฆ๊ฐ ์—ฐ์‚ฐ์ž์˜ ๊ฐ’์„ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ๋Œ€์ž…
        int result1 = num1++;
        System.out.println("result1 after post-increment: " + result1); // ์ถœ๋ ฅ: result1 after post-increment: 6
        System.out.println("num1 after post-increment: " + num1); // ์ถœ๋ ฅ: num1 after post-increment: 7

        int result2 = ++num2;
        System.out.println("result2 after pre-increment: " + result2); // ์ถœ๋ ฅ: result2 after pre-increment: 10
        System.out.println("num2 after pre-increment: " + num2); // ์ถœ๋ ฅ: num2 after pre-increment: 10
    }
}


์ฆ๊ฐ ์—ฐ์‚ฐ์ž์™€ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ „์œ„์™€ ํ›„์œ„ ์—ฐ์‚ฐ์˜ ์ฐจ์ด์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ฆ๊ฐ ์—ฐ์‚ฐ์ž์˜ ํ›„์น˜ ์—ฐ์‚ฐ์ž์™€ ์ „์น˜ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•˜์‹œ๋‹ค๋ฉด ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”๐Ÿ‘

" "

[Java]์ž๋ฐ” ๋ถ€ํ˜ธ ๋ฐ ์ฆ๊ฐ ์—ฐ์‚ฐ์ž ํ™œ์šฉ

๋ชฉ์ฐจ1. ๋ถ€ํ˜ธ ์—ฐ์‚ฐ์ž2. ์ฆ๊ฐ ์—ฐ์‚ฐ์ž3. ๋ถ€ํ˜ธ/์ฆ๊ฐ ์—ฐ์‚ฐ์ž ํ™œ์šฉ4. ํ•ต์‹ฌ ๋‚ด์šฉ<span style="background-color: #ffffff; color: #0d0d0d; text-align: start;..

creativevista.tistory.com


๋Œ€์ž… ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”, ์กฐ๊ฑด๋ฌธ์—์„œ์˜ ๋ณ€์ˆ˜ ํ• ๋‹น ๋“ฑ ๋‹ค์–‘ํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์‹ค์ œ ํ™œ์šฉ ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public class AssignmentOperatorExample {
    public static void main(String[] args) {
        // ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”
        int a = 5;
        int b = 3;
        int result = 0;

        // ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™” ๋ฐ ๊ฐ’ ๋ณ€๊ฒฝ
        result = a; // ๋ณ€์ˆ˜ a์˜ ๊ฐ’์„ result์— ๋Œ€์ž…
        System.out.println("result after initialization: " + result); // ์ถœ๋ ฅ: result after initialization: 5

        // ์กฐ๊ฑด๋ฌธ์—์„œ์˜ ๋ณ€์ˆ˜ ํ• ๋‹น
        boolean condition = (a > b); // a๊ฐ€ b๋ณด๋‹ค ํฐ์ง€ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ๋ฅผ condition์— ๋Œ€์ž…
        result = (condition) ? a : b; // condition์ด ์ฐธ์ด๋ฉด a๋ฅผ, ๊ฑฐ์ง“์ด๋ฉด b๋ฅผ result์— ๋Œ€์ž…
        System.out.println("result after conditional assignment: " + result); // ์ถœ๋ ฅ: result after conditional assignment: 5

        // ๋ฐ˜๋ณต๋ฌธ์—์„œ์˜ ๋ณ€์ˆ˜ ํ• ๋‹น
        for (int i = 0; i < 5; i++) {
            result += i; // result์— i๋ฅผ ๋”ํ•˜์—ฌ ๊ฐ’ ๊ฐฑ์‹ 
        }
        System.out.println("result after loop: " + result); // ์ถœ๋ ฅ: result after loop: 15
    }
}
  1. ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”: ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. (result = a;)
  2. ์กฐ๊ฑด๋ฌธ์—์„œ์˜ ๋ณ€์ˆ˜ ํ• ๋‹น: ์กฐ๊ฑด๋ฌธ ๋‚ด์—์„œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•  ๋•Œ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    (result = (condition) ? a :b;)
  3. ๋ฐ˜๋ณต๋ฌธ์—์„œ์˜ ๋ณ€์ˆ˜ ํ• ๋‹น: ๋ฐ˜๋ณต๋ฌธ ๋‚ด์—์„œ ๊ฐ’์„ ํ• ๋‹นํ•˜๊ฑฐ๋‚˜ ๊ฐฑ์‹ ํ•  ๋•Œ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    (result += i;)

์•„๋ž˜ ์˜ˆ์ œ๋Š” ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ๊ณผ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๋Š” ํŒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

public class AssignmentOperatorExample {
    public static void main(String[] args) {
        // ์ฃผ์˜ํ•  ์ : ๋ณ€์ˆ˜์˜ ๊ฐ’ ๋ณ€๊ฒฝ
        int x = 5;
        int y = x;
        System.out.println("Before: x=" + x + ", y=" + y); // ์ถœ๋ ฅ: Before: x=5, y=5
        x = 10; // x์˜ ๊ฐ’์„ ๋ณ€๊ฒฝ
        System.out.println("After: x=" + x + ", y=" + y); // ์ถœ๋ ฅ: After: x=10, y=5

        // ๊ฐ€๋…์„ฑ์„ ๋†’์ด๋Š” ํŒ: ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ
        int a = 5;
        int b = 3;
        
        // ๊ฐ€๋…์„ฑ์ด ๋†’๊ณ  ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ
        a += b; // a = a + b;
        System.out.println("a after +=: " + a); // ์ถœ๋ ฅ: a after +=: 8
        
        // ๊ฐ€๋…์„ฑ์ด ๋‚ฎ๊ณ  ๋ณต์žกํ•œ ์ฝ”๋“œ
        int c = 5;
        int d = 3;
        c = c + d;
        System.out.println("c after +: " + c); // ์ถœ๋ ฅ: c after +: 8
    }
}
  1. ์ฃผ์˜ํ•  ์ : ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ณ€์ˆ˜ ํ• ๋‹น ์ „๊ณผ ํ›„์˜ ๊ฐ’์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  2. ๊ฐ€๋…์„ฑ์„ ๋†’์ด๋Š” ํŒ: ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ„๊ฒฐํ•ด์ง€๊ณ  ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง‘๋‹ˆ๋‹ค.

4.ํ•ต์‹ฌ ๋‚ด์šฉ๐Ÿ‘€

์ž๋ฐ” ๋Œ€์ž…์—ฐ์‚ฐ์ž

728x90
๋ฐ˜์‘ํ˜•