ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” ์—ฐ์‚ฐ์ž 6๊ฐ€์ง€ Operator
    JAVA 2020. 7. 29. 08:30

    ๐ŸŽฏ ์ž๋ฐ”์˜ 6๊ฐ€์ง€ ์—ฐ์‚ฐ์ž (์‚ฐ์ˆ , ๋Œ€์ž…, ์ฆ๊ฐ, ๋น„๊ต, ๋…ผ๋ฆฌ, 3ํ•ญ ์—ฐ์‚ฐ์ž)๋ฅผ ์•Œ์•„๋ณธ๋‹ค.

     


     

     

    ์—ฐ์‚ฐ์ž (Operator)

     

    ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž

    public class sample05_operator {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            //์‚ฐ์ˆ ์—ฐ์‚ฐ์ž
    		int n = 10;
    		int n2 = 3;
    		System.out.println("n+n2 = " + n + n2);
    		System.out.println("n-n2 = " + (n - n2));
    		System.out.println("n*n2 = " + n * n2);
    		System.out.println("n/n2 = " + n / n2);
    		System.out.println("n/3.0 = " + n / 3.0);
    		System.out.println("n%n2 = " + n % n2);
        }
    
    }

     

     

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

    		int x = 10;
    		int x2 = x;
    		x2 += x;  // x2 = x2 + x;
    		x2 -= x;  // x2 = x2 - x; 
    		x2 *= x;  // x2 = x2 * x;
    		x2 /= x;  // x2 = x2 / x;
    		x2 %= x;  // x2 = x2 % x;
            
    		System.out.println();

     

     

    ์ฆ๊ฐ์—ฐ์‚ฐ์ž

    1์”ฉ ์ฆ๊ฐ€ ๋˜๋Š” ๊ฐ์†Œ์‹œํ‚ค๋Š” ์—ฐ์‚ฐ์ž

            int k =10;
            ++k;  //์ „์น˜
            System.out.println(k);
            k++;  //ํ›„์น˜
            System.out.println(k);

    ++a ๋ณ€์ˆ˜์•ž์— ๋ถ™์œผ๋ฉด ์ „์น˜, ์ „์œ„ ์—ฐ์‚ฐ์ž. ์—ฐ์‚ฐํ•˜๊ธฐ ์ „์— ๋ณ€์ˆ˜๊ฐ’์„ ๋จผ์ € ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. a์— 1์„ ๋จผ์ € ์ฆ๊ฐ€ํ•˜๊ณ  ์—ฐ์‚ฐ.

    a++ ๋ณ€์ˆ˜๋’ค์— ๋ถ™์œผ๋ฉด ํ›„์น˜, ํ›„์œ„ ์—ฐ์‚ฐ์ž. ์—ฐ์‚ฐ ํ›„ ๋ณ€์ˆ˜๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. a๋ฅผ ์—ฐ์‚ฐํ•˜๊ณ  1 ์ฆ๊ฐ€.

    —a a— -- ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์†Œ

    int x = ++n; n๊ฐ’์„ ๋จผ์ € ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ๋‚˜์ค‘์— x์— ํ• ๋‹นํ•œ๋‹ค.

    int x = n++; n๊ฐ’์„ ๋จผ์ € ํ• ๋‹นํ•˜๊ณ  ๋‚˜์ค‘์— n๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

     

            int k =10;
            int a = ++k;  //์ „์น˜ //k = k+1, int a = k
            System.out.println(a+ " "+k);  //11 11
            int b = k++;  //ํ›„์น˜ //int b = k, k = k+1;
            System.out.println(b+ " "+k);  // 11 12

    k๋Š” ++์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 1์ด ์ฆ๊ฐ€๋œ ์ƒํƒœ์ด๋‹ค. //11

    ๊ทธ ๋‹ค์Œ k++์€ 11์„ ๋จผ์ € b์— ํ• ๋‹นํ•˜๊ณ  ๊ทธ ๋‹ค์Œ +1 ์ฆ๊ฐ€์ด๊ธฐ ๋•Œ๋ฌธ์— k๊ฐ’์€ 12๊ฐ€ ๋œ๋‹ค.

            int k =10;
            int a = --k;  //์ „์น˜ //k = k-1, int a = k
            System.out.println(a+ " "+k);
            int b = k--;  //ํ›„์น˜ //int b = k, k = k-1;
            System.out.println(b+ " "+k);

    ๊ฐ์†Œ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

    ์ „์น˜๊ฐ์†Œ๋Š” ๋จผ์ € ๋งˆ์ด๋„ˆ์Šค ํ•˜๊ณ  ํ• ๋‹น์ด๋‹ˆ๊นŒ a = 9, k = 9 ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค. ํ›„์น˜๊ฐ์†Œ๋Š” b=9 ํ• ๋‹นํ•˜๊ณ  -1์ด๋‹ˆ๊นŒ k = 8์ด ๋œ๋‹ค.

            int y=10;
            int y2 = ++y;
            System.out.println(y+"\t"+y2); //11 11
            int y3 = y++;
            System.out.println(y+"\t"+y3); //11 12

    ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ++ ๋ถ™๋Š” ์œ„์น˜์— ๋”ฐ๋ผ ํ• ๋‹น๋จผ์ €์ธ์ง€ ์ฆ๊ฐ€ ๋จผ์ €์ธ์ง€๋ฅผ ๊ธฐ์–ตํ•˜๋ฉด ๋œ๋‹ค.

     

     

    ๋น„๊ต์—ฐ์‚ฐ์ž

    //  ๋น„๊ต์—ฐ์‚ฐ์ž==> ์‹คํ–‰๊ฒฐ๊ณผ๋Š” ๋…ผ๋ฆฌ๊ฐ’ t or f
    
            int xyz=5;
            int xyz2=3;
            boolean result = xyz == xyz2;
            System.out.println(xyz == xyz2);  //false
            System.out.println(xyz != xyz2);  //true
            System.out.println(xyz > xyz2);  //true
            System.out.println(xyz >= xyz2);  //true
            System.out.println(xyz < xyz2);  //false
            System.out.println(xyz <= xyz2);  //false

    ๋น„๊ต์—ฐ์‚ฐ์ž์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” true ๋˜๋Š” false๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

     

     

    ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž

    && : and ํ•˜๋‚˜๋ผ๋„ ๊ฑฐ์ง“์ด๋ฉด ๊ฑฐ์ง“. ๋ชจ๋‘๊ฐ€ ์ฐธ์ด์—ฌ์•ผ ์ฐธ๋‚˜์˜จ๋‹ค

    ||: or ๋‘˜์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฐธ์ด๋ฉด ์ฐธ ๋‘˜๋‹ค ๊ฑฐ์ง“์ด๋ฉฐ ใ„ด๊ฑฐ์ง“

    ! : not ๋ถ€์ •

            System.out.println( 3==4 && 4>3);
    
            System.out.println( ! true );
            System.out.println( ! false );
    
            System.out.println(true || true);
            System.out.println(true || false);
            System.out.println(false || true);
            System.out.println(false || false);
    
            System.out.println(true && true);
            System.out.println(true && false);
            System.out.println(false && true);
            System.out.println(false && false);
                    int x = 10;
                    int y = 15;
                    System.out.println(x > 5 && x < 25 );
                    System.out.println(x > 8 && x < 10);
    
                    System.out.println(x > 15 || x <25);
                    System.out.println(x > 8 || y < 25);

     

    short circuit operation

    ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ์ฒซ๋ฒˆ์งธ ์กฐ๊ฑด๋งŒ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋‘๋ฒˆ์งธ๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์งง์€ ์‹คํ–‰์œผ๋กœ ๋…ผ๋ฆฌ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

        String name = null;
    //    System.out.println((4 < 2) && (name.length() == 4));
        System.out.println((4 > 2) && (name.length() == 4));

    ์ฒซ๋ฒˆ์งธ ํ”„๋ฆฐํŠธ์—์„œ NulPointerException ์—๋Ÿฌ๊ฐ€ ๋– ์•ผํ•˜๋Š”๋ฐ &&์—ฐ์‚ฐ์ž๋กœ ์ด์–ด์ง„ ์ฒซ์กฐ๊ฑด์ด true๋ผ์„œ ๋‘๋ฒˆ์งธ ์กฐ๊ฑด์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ  ์‹คํ–‰์ด ๋๋‚ฌ๋‹ค. ๋‘๋ฒˆ์งธ ํ”„๋ฆฐํŠธ๋Š” ์ฒซ์กฐ๊ฑด์ด false๋ผ์„œ ๋‹ค์Œ ์กฐ๊ฑด์„ ์ˆ˜ํ–‰ํ–ˆ๋‹ค. ๋„๊ฐ’์€ ๊ธธ์ด๋ฅผ ์…€ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์—๋Ÿฌ๊ฐ€ ๋‚˜์•ผํ•œ๋‹ค.

     

     

    3ํ•ญ์—ฐ์‚ฐ์ž

    ๋ณ€์ˆ˜ = (์กฐ๊ฑด์‹) ? ์ฐธ๊ฐ’ : ๊ฑฐ์ง“๊ฐ’;

    ์กฐ๊ฑด์‹์ด true์ด๋ฉด ์ฐธ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅ, ์กฐ๊ฑด์‹์ด false์ด๋ฉด ๊ฑฐ์ง“๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค.

    int xxx =(3==4)?100:200;
    System.out.println(xxx);

    ์กฐ๊ฑด์‹์ด false์ด๋‹ˆ๊นŒ ๊ฑฐ์ง“๊ฐ’์— ๋Œ€์‘ํ•˜๋Š” 200์ด xxx์˜ ๊ฐ’์ด ๋œ๋‹ค.

    		int xxx =(3==4)?100:200;
    		String xxx2 =(3==4)?"A":"B";
    		System.out.println(xxx); //200
    		System.out.println(xxx2); //B
    // ๋‘ ์ •์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ
    
    				int a = 10;
    				int b = 20;
    				int max = (a < b) ? b:a;
    				System.out.println(max);
    // 3๊ฐœ์˜ ๋ณ€์ˆ˜๊ฐ’ ์ค‘์—์„œ ์ตœ๋Œ€๊ฐ’ ๊ตฌํ•˜๊ธฐ
    
    				int a = 12;
    				int b = 4;
    				int c = 19;
    				
    				int max = (a > b) ? a : b;
    				    max = ( max > c ) ? max : c;
    				System.out.println( "์ตœ๋Œ“๊ฐ’์€ " + max ); //19

    ๋ณ€์ˆ˜๋Š” ์–ธ์ œ๋“ ์ง€ ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜๋‹ค. max ๋ณ€์ˆ˜์— ์ตœ๋Œ“๊ฐ’๋งŒ ์ €์žฅํ•˜๋Š” ๋‘ ๊ฐœ์˜ 3ํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ๋‘ ์—ฐ์‚ฐ์กฐ๊ฑด์œผ๋กœ max์—๋Š” ์ตœ๋Œ“๊ฐ’์ด ์ž…๋ ฅ๋œ๋‹ค.

     

     

     

    ๐Ÿ˜œ NEXT : ์ž๋ฐ” ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ ๊ณต๋ถ€ํ•˜๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€