반응형

           

  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


반응형

+ Recent posts