반응형

           

  JAVA Study






- 산술 연산

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class tistory {
    public static void main(String args[]{
 
        int a = 150;
        int b = 7;
 
        int c = a+b;
 
        System.out.println(c); //157
        
        System.out.println("result1 = " + (a+b)); // result1 = 157
        System.out.println("result2 = " + (a-b)); // result2 = 143
        System.out.println("result3 = " + (a*b)); // result3 = 1050
        System.out.println("result4 = " + (a/b)); // result4 = 21
        System.out.println("result5 = " + (a%b)); // result5 = 3
 
    }
}
 
cs



- 연결 연산자

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class tistory {
 
    public static void main(String[] args) {
        
        String color1 = "파랑";
        String color2 = "빨강";
        
        System.out.println("나는 "+color1+"색을 좋아한다."); // 나는 파랑 색을 좋아한다.
        System.out.println("신호등은 "+color2+"에서 멈춘다."); // 나는 빨강에서 멈춘다.
 
    }
 
}
 
 
cs



- 증감 연산 / 가감 연산

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class tistory {
 
    public static void main(String[] args) {
        
        int a = 1;
        
        ++a; //2
        a++//3
 
        System.out.println(a); //3
        
        int b = 5;
        
        --b; //4
        b--//3
        
        System.out.println(b); //3
    }
 
}
 
cs



- 대입 연산

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class tistory {
 
    public static void main(String[] args) {
        
        int a = 30;
 
        a+=5// 35
        System.out.println(a); //35
        
        a-=10// 25
        System.out.println(a); //25
        
        a*=2//50
        System.out.println(a); //50
        
    }
 
}
cs



- 삼항 연산자 (조건 연산자)


1
2
3
4
5
6
7
8
9
10
11
12
13
public class tistory {
 
    public static void main(String[] args) {
        
        String s1 = "apple";
        String s2 = "samsung";
    
        String s3 = (10%2 == 0)?s1 :s2; // 조건식 (10%2 == 0) 참일때 s1을 실행, 거짓일 때 s2를 실행
        System.out.println(s3); //apple
            
    }
 
}
cs



- 관계 연산자 (크기비교)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class tistory {
 
    public static void main(String[] args) {
        
        int num1 = 10;
        int num2 = 20;
        
        System.out.println(num1 +">" +num2 +"=" +(num1>num2)); //10>20 = false
        System.out.println(num1 +"<" +num2 +"=" +(num1<num2)); //10<20 = true
        System.out.println(num1 +">=" +num2 +"=" +(num1>=num2)); //10>=20 = false
        System.out.println(num1 +"<=" +num2 +"=" +(num1<=num2)); //10<=20 = true
        System.out.println(num1 +"==" +num2 +"=" +(num1==num2)); //10==20 = false
        System.out.println(num1 +"!=" +num2 +"=" +(num1!=num2)); //10!=20 = true
            
    }
 
}
cs



- 논리 연산자 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class tistory {
 
    public static void main(String[] args) {
        
        int num1 = 10;
        int num2 = 20;
        
        //And
        System.out.println(num1<num2 && num1!=num2); // true
        System.out.println(num1>num2 && num1!=num2); // false
        
        //OR
        System.out.println(num1<num2 || num1!=num2); // true
        System.out.println(num1>num2 || num1!=num2); // true
        System.out.println(num1>num2 || num1==num2); // false
 
        //NOT
        System.out.println(!(num1<num2)); // false
        System.out.println(!(num1>num2)); // true
        
    }
 
}
cs


반응형
반응형

           

  JAVA Study





1. 연산자(operator) 


- 연산을 수행하는 기호


- 연산자

: 연산을 수행하는 기호 ( +, -, *, /, % )


- 피연산자

: 연산자의 작업대상 ( 변수, 상수, 리터럴, 수식 )


- 연산자는 피연산자로 연산을 수행 후 항상 결과값을 반환


- 연산자의 종류

 종류

 연산자 

 설명 

 산술 연산자

    +        -        *         /       %      <<      >>

 사칙연산과 나머지 연산(%)

 비교 연산자

    >      <       >=       <=      ==       !=

 크고 작음, 같고 다름을 비교

 논리 연산자

   &&      ||       !      &       |       ^      ~

 'and'와 'or'로 조건 연결

 대입 연산자

   = 

 우변의 값을 좌변에 저장

 기타

   (type)             ? :               instanceof 

 형변환연산자, 삼항연산자, instanceof연산자



- 연산자의 우선순위

: 산술 > 비교 > 논리 > 대입

: 단항 > 이항 > 삼항

: 단항 연산자와 대입 연산자를 제외한 모든 연산의 진행방향은 왼쪽에서 오른쪽




2. 단항 연산자 
 

- 증감 연산자 

: ++  피연산자의 값을 1 증가

: --  피연산자의 값을 1 감소 

: 전위형 (++i) - 값이 참조되기 전에 증가

: 후위형 (i--) - 값이 참조된 후에 증가



3. 산술 연산자 

- 사칙 연산자 (+-*/)


- 나머지 연산자 (%)

: 나누는 수는 0을 사용할 수 없고, 피연산자로 정수만 허용
 


4. 비교 연산자 
 

- > 좌변 값이 크면 true 아니면 false


- < 좌변 값이 작으면 true 아니면 false


- >= 좌변 값이 크거나 같으면 true 아니면 false


- <= 좌변 값이 작거나 같으면 true 아니면 false


- == 두 값이 같으면 true 아니면 false


- != 두 값이 다르면 true 아니면 false


- 문자열의 비교

: equalse() 메서드 사용

 ( ex. 변수이름.equalse("abc"); )



5. 논리 연산자 
 

- || (or) 피연산자 중 어느 한쪽만 true이면 결과는 true


- && (and) 피연산자 양쪽모두 true이어야 결과는 true


- | (or 연산자) 피연산자 중 한쪽 값이 1이면, 1을 결과로 얻음, 그 외에는 0


- & (and연산자) 피연사 양 쪽 모두 1이어야 1을 결과로 얻음, 그 외에는 0


- ^ (xor연산자) 피연산자의 값이 서로 다를 때만 1을 결과로 얻음. 같을 때는 0


- ~ (비트 전환 연산자) 피연산자의 1의 보수를 얻을 수 있음, 부호있는 타입에서는 반대부호로 변경


- << ,  >> (쉬프트 연산자) 2진수로 표현된 피연산자의 각 자리를 오른쪽(>>), 왼쪽(<<)으로 이동함



6. 그 외의 연산자 


- ? / : (조건 연산자) 

: '(조건식) ? 식1 : 식2' 형식의 삼항 연산자, 조건식이 참일 경우 '?' 이하 식1을 실행, 거짓일 경우 ':' 이하 식2를 실행

: if 문으로 바꿔 사용가능


- = , op= (대입 연산자)

: 변수같은 저장공간에 값 또는 수식의 연산결과를 저장하는데 사용

: 오른쪽 피연산자 ( rvalue ) 의 값을 왼쪽 피연산자 ( lvalue ) 에 저장

: 대입 연산자는 다른연산자 ( op )와 결합하여 'op='형식으로 사용가능 ( ex. i = i+2; --> i+=2; )

: op=연산자는 반드시 공백없이 붙여써야 함  



 자바의 정석 저자 강의

- 연산자

반응형

+ Recent posts