ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] Operator
    Programming Lang/Java 2022. 8. 27. 02:03

    연산자란?

    Operator (연산자)
    a symbol that does something to a number or quantity in a calculation.
    For example, in 7 + y, the symbol + is the operator.
    계산에서 숫자나 양에 어떤 일을 하는 기호.
    예를 들어, 7 + y에서 + 기호는 연산자 이다.


    ex) + - * /
    Operand (피연산자)
    a number or quantity that has something done to it in a calculation.
    For example, in 7 + y, 7 and y are the operands.
    계산에 어떤 영향을 미치는 수 또는 수량.
    예를 들어, 7 + y에서 7과 y는 피연산자 이다.


    ex) variable, constant, literal

     

    산술 :     +   -   *   /   %   <<   >>

    비교 :     >   <   >=   <=   ==   !=

     

    논리 :     &&   ||   !   &   |   ^   ~

    삼항 :     ?   :

    대입 :     =   +=   -=   *=   /=   %=   <<=   >>=   &=   ^=   |=

    기타(단항) :     ++   --   +   -   ~   !   (type)

     

    산술 > 비교 > 논리 > 삼항 > 대입

    class JavaOperator {
    
        // ;가 없는 것은 식
        // ex> 4*x+3
        // 결과는 있지만 소멸된다.
    
        // ; 까지 있는 것은 문장
        // ex> 4*x+3;
        // 결과는 있지만 소멸된다.
    
        // 등호가 있는 것은 대입
        // 보통 오른쪽에서 왼쪽 방향으로 해석한다.
    
        // 4*x+3는 y이다.
        // ex> y = 4*x+3;
        // 결과를 변수에 담아준다.
    
        // ex> System.out.println(4*x+3);
        // ex> System.out.println(y);
        // 결과를 화면 콘솔에 출력해준다.
    
        public static void main(String[] args) {
    
            //1. 우선순위
            first();
    
            //2. 단항
            second();
    
            //3. 산술
            third();
    
        } // main
    
    
        //1. 우선순위 & 4. 비교
        public static void first() {
    
            System.out.println("[ ============ first 우선순위 ============ ]");
    
            // 산술 > 비교 > 논리 > 삼항 > 대입
            // 산술 : 사칙연산, 나머지, 쉬프트 연산자
            // 비교 : and, or 순
    
            int x = 1;
            int y = 0;
    
            //x의 앞은 마이너스가 아니라 부호연산자이다.
            //하지만 결과는 같다.
            if(-x+3 == 2) {
                System.out.println(-x+3);
            }
    
            //사칙연산과 똑같이 곱셈을 먼저 계산한다.
            if(x+3*y == 1) {
                System.out.println(x+3*y);
            }
    
            //비교 부등호보다 산술연산자를 먼저 계산한다.
            if(x+3>y-2) {
                System.out.println("x+3이 y-2보다 크다");
            }else{
                System.out.println("x+3이 y-2보다 크지 않다");
            }
    
            //논리연산자보다 비교연산자가 먼저 수행된다.
            if(x>3 && x<5) {
                System.out.println("x가 3보다 크고 5보다 작다");
            }else{
                System.out.println("x가 3보다 크고 5보다 작지 않다");
    
            }
    
            //산술, 비교, 논리 순으로 수행된다.
            if(x-2<3 || y+2>5) {
                System.out.println("x-2가 3보다 작고 y+2가 5보다 작다");
            }else{
                System.out.println("x-2가 3보다 작고 y+2가 5보다 작지 않다");
            }
    
            System.out.println("\n");
    
        } // first()
    
    
        //2. 단항
        public static void second() {
        	System.out.println("[ ============ second 단항 ============ ]");
    
        	//후위형
            int i=5;
            System.out.println("후위"+i);
            i++;
            System.out.println(i);
    
            //전위형
            i=5;
            System.out.println("전위"+i);
            ++i;
            System.out.println(i);
    
        System.out.println("\n");
    
        } // second()
    
    
        //3. 산술
        public static void third() {
    
        	System.out.println("[ ============ third 산술 ============ ]");
    
        	//연산 직전 자동형변환 발생
    	//int보다 작은 타입은 int로 변환 : 정수로 나옴
    
            byte aaa = 1;
            short bbb = 1;
            char ccc = 1;
    
            System.out.println(aaa+ccc);
            System.out.println(bbb+ccc);
    
            //int이상의 타입들은 더 큰타입으로 변환
            int iii = 10;
            float fff = 10.111F;
            double ddd = 10.111;
            long lll = 10;
    
            System.out.println(iii+lll); //int + long
            System.out.println(fff+iii);//float + int
            System.out.println(ddd+fff);//double + float
    
            System.out.println("\n");
            
        } // third()
    } // JavaOperator class

    'Programming Lang > Java' 카테고리의 다른 글

    [Java] Inheritance, Overriding  (0) 2022.09.09
    [Java] Class, Instance, Constructor  (0) 2022.09.07
    [Java] Variable, Method, JVM  (0) 2022.09.05
    [Java] String Array  (0) 2022.08.28
    [Java] Type Casting  (0) 2021.07.01
Designed by Tistory.