S.Record
article thumbnail

OperatorEx26.java

public class OperatorEx26 {
	public static void main(String[] args) {
		// |(비트연산자), ||
		int i = 0;
		System.out.println(0 == i || 3/i == 2); // 3/i 는 에러가 뜨는 것이지만 OR 연산은 // 좌항이 true
												//  첫번째 항이 true 면 뒤의 항을 연산하지 않는다
//		System.out.println(0 != i || 3/i == 2); // 좌항이 false라서 우항은 에러가 뜨는 연산이기때문에 // 좌항이 false
												// 위와는 달리 에러가 뜬다.
		System.out.println(0 != i && 3/i == 2); // 좌항이 false // AND 연산자는 하나라도 false면 false를 출력하기 때문에
												// 
		}
}

결과값


OperatorEx27.java

public class OperatorEx27 {
	public static void main(String[] args) {
		boolean b = true;
		char ch = 'C';
		
		System.out.printf("b = %b%n", b);
		System.out.printf("!b = %b%n", !b); // true 를 부정 -> false
		System.out.printf("!!b = %b%n",!!b); // !b = false -> !!b true
		System.out.printf("!!!b = %b%n", !!!b);
		System.out.println();
		
		System.out.printf("ch = %c%n", ch);
		System.out.printf("ch < 'a' || ch > 'z' = %b%n",
													ch< 'a' || ch > 'z');
		System.out.printf("!('a' <= ch && ch <= 'z') = %b%n",
													 !('a' <= ch && ch <= 'z'));
		System.out.printf("  'a' <= ch && ch <= 'z'  = %b%n",
														'a' <= ch && ch <= 'z');
		
		// 드 모르간의 법칙
//		!(A && B) = !A || !B  
//		!(A && !B) = !A || B
	}
}

결과값


Operator01.java

public class Operator01 { //비트연산자
	public static void main(String[] args) {
		System.out.println(10 & 6); // 10 = 1010, 6 =0110
		System.out.println(10 | 6); // 
		System.out.println(10 ^ 6);
		
		// 1010 : 10
		// 0110 : 6
		
		//&, |, ^
		// AND & => 0010 : 2
		//  OR | => 1110 : 14
		// XOR ^ => 1100 : 12
		
		// ~ (틸트) 부정 , 0과 1을 바꿈
		System.out.println("=====================");
		System.out.println(10); // 32 bit
		System.out.println(~10);
		
		System.out.println("0000000000000000000000000000"
							+Integer.toBinaryString(10));
		System.out.println(Integer.toBinaryString(~10));
		// << >> >>> (시프트연산자)
		System.out.println("=====================");
		System.out.println(10);			// 시프트 연산자는 2진수 위치를 옮긴다.
		System.out.println(10 << 2); 	// 왼쪽으로   2칸 
		System.out.println(10 >> 2); 	// 오른쪽으로 2칸
		System.out.println(10 >>> 2);	//
		System.out.println();
		
		System.out.println(-10);		//
		System.out.println(-10 << 2); 	// 왼쪽으로 갈때는 오른쪽을 0으로 채우고 
		System.out.println(-10 >> 2); 	// 오른쪽으로 할때는 왼쪽에 부호로 채운다. - 의 경우 1로 채움
		System.out.println(-10 >>> 2);	// 숫자에 따라 부호를 1 -> 0 으로 바꾸어서 +,- 가 바뀐다.
		System.out.println();
		
		System.out.println(Integer.toBinaryString(-1024));
		System.out.println(Integer.toBinaryString(-1024 << 2));
		System.out.println(Integer.toBinaryString(-1024 >> 2));
		System.out.println("00" +Integer.toBinaryString(-1024 >>> 2));
	}
}

결과값


Operator02.java

public class Operator02 { //조건(삼항)연산자
	public static void main(String[] args) {
		int i = 0;
		String result = i > 0 ? "양수 입니다" : i == 0 ? "0" : "음수 입니다." ;
		
		System.out.println(result);
		
		if(i >0) { // 삼항연산자보다 조건문이 2-3배 더 빠르다 , 되도록 조건문으로
			result = "양수 입니다";
		}
		else {
			if(i==0) {
				
				result = "0";		
			}
			else {
				result = "음수 입니다";
			}
		}
		System.out.println(result);
		
		int a1 =333;
		System.out.println(a1 % 2 == 0 ? "짝수" : "홀수");
		String a2 = (i % 2) == 0 ? "짝수" : "홀수";
		System.out.println(a2);
		
	}
}

결과값


Operator03.java

public class Operator03 {
	public static void main(String[] args) {
		int x, y;
		x = y = 3;
		
//		lvalue(left value, 좌항값), rvalue(right value, 우항값)
//		리터럴이나 상수같이 값을 저장할 수 없는 것들은 lvalue가 될 수 없다.
		
		String str = "";
		str += "안녕하세요 \n"; // a += "문자열" => a = a + "문자열"
		str += "이제 연산자가 끝납니다. \n";
		str += "제어문을 시작하도록 하겠습니다.";
		
		System.out.println(str);
		
	}
}

결과값

 

'dev.lang > Java' 카테고리의 다른 글

190617. 4장 조건문과 반복문. 예제. //주석  (0) 2019.06.17
190614. 3장 연산자(Operator). 예제 //주석포함  (0) 2019.06.17
190613  (0) 2019.06.13
190612  (0) 2019.06.12
Day.2 .190611  (0) 2019.06.11
profile

S.Record

@seuhyang

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!