조건 연산자? :

조건 연산자는 조건식, 식 1, 식 2 모두 세 개의 피연산자를 필요로 하는 삼항 연산자이며,

삼항 연산자는 조건 연산자 하나뿐이다.

 

(조건식)? (참이면 반환) : (거짓이면 반환)

 

public class OperatorEx32 {
	public static void main(String[] args) {
		int x, y, z;
		int absX, absY, absZ;
		char signX, signY, signZ;
		
		x = 10;
		y = -5;
		z = 0;
		
		absX = x >= 0 ? x : -x; // x의 값이 음수이면, 양수로 만든다.
		absY = y >= 0 ? y : -y;
		absZ = z >= 0 ? z : -z;
		
		signX = x > 0 ? '+' : (x==0 ? ' ' : '-'); //조건 연산자를 중첩
		signY = y > 0 ? '+' : (y==0 ? ' ' : '-');
		signZ = z > 0 ? '+' : (z==0 ? ' ' : '-'); 
		
		System.out.printf("x=%c%d%n", signX, absX);
		System.out.printf("y=%c%d%n", signY, absY);
		System.out.printf("z=%c%d%n", signZ, absZ);
		
	}

}
x=+10
y=-5
z= 0

 

대입 연산자 = op=

대입 연산자는 변수와 같은 저장공간에 값 또는 수식의 연산 결과를 저장하는 데 사용된다.

특이한 점은 연산 진행방향이 오른쪽에서 왼쪽이다.

대입 연산자의 왼쪽 피연산자를 lvalue(left value)이라 하고, 오른쪽 피연산자를'rvalue(right value)라고 한다.

 

복합 대입 연산자

대입 연산자는 다른 연산자(op)와 결합하여 op=과 같은 방식으로 사용될 수 있다.

 

op= =
i +=3; i = i + 3;
i -= 3; i = i - 3;
i *= 3; i = i * 3;
i /= 3; i = i / 3;
i %= 3; i = i % 3;
i <<= 3; i = i << 3;
i >>= 3; i = i >> 3;
i &= 3; i = i & 3;
i ^= 3; i = i ^ 3;
i |= 3; i = i | 3;

비트 연산자(bitwise operator)

비트 연산자는 피연산자를 비트단위로 논리 연산한다. 

그런데 쓸 일이 거의 없으니 그냥 교양 정도로 보면 된다.

|(OR연산자) 피연산자 중 한 쪽의 값이 1이면, 1을 결과로 얻는다. 그 외에는 0을 얻는다.
&(AND연산자) 피연산자 양 쪽이 모두 1이어야만 1을 결과로 얻는다. 그 외에는 0을 얻는다.
^(XOR연산자) 피연산자의 값이 서로 다를 때만 1을 결과로 얻는다. 같을 때는 0을 얻는다.
x y x|y x&y x^y
1 1 1 1 0
1 0 1 0 1
0 1 1 0 1
0 0 0 0 0

연산자 ^는 배타적 XOR(eXclusive OR)라고 하며, 피연산자의 값이 서로 다른 경우, 즉 배타적인 경우에만 참(1)을 결과로 얻는다.

 

비트 연산자    
0xAB | 0xF = 0xAF
1 0 1 0 1 0 1 1
|)        0 0 0 0 1 1 1 1

0xAB
0xF
1 0 1 0 1 1 1 1 0xAF

|(OR)는 주로 특정 비트의 값을 변경할 때 사용한다.

 

 

비트 연산자    
0xAB & 0xF = 0xB
1 0 1 0 1 0 1 1
&)     0 0 0 0 1 1 1 1
0xAB
0xF
0 0 0 0 1 0 1 1 0xB

&(AND)는 주로 특정 비트의 값을 뽑아낼 때 사용한다.

 

 

비트 연산자    
0xA4 ^ 0xF = 0xAB
1 0 1 0 0 1 0 0
 ^)    0 0 0 0 1 1 1 1
0xA4
0xF
1 0 1 0 1 0 1 1 0xAB

^(XOR)는 두 피연산자의 비트가 다를 때만 1이 된다.

 

 

비트 전환 연산자 ~

x ~x
1 0
0 1

~는 주로 특정 비트의 값을 뽑아낼 때 사용한다.

주의할 점은 비트 연산자 '~'에 의해 '비트 전환'되고 나면, 부호가 있는 타입의 피연산자는 부호가 반대로 변경된다.

 

쉬프트 연산자 << >>

이 연산자는 피연산자의 각 자리를 이동한다고해서 쉬프트 연산자(shift operator)라고 이름 붙여졌다.

 

10진수 8은 2진수로표현한 뒤 쉬프트 연산자를 적용하면,

 

  << >>
연산전 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0
연산후 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0

각 각 연산 후 8이 16과 4가 되었다.

또한 쉬프트 연산자의 범위를 벗어난 값은 버려지고, 0이 된다. 이로써 알 수 있는 것은

x << n은 x * 2ⁿ의 결과와 같다.

x >> n은 x / 2ⁿ의 결과와 같다.

비교 연산자는 두 피 연산자를 비교하는데 사용되는 연산자다.

연산 결과는 boolean타입인 true와 false로 출력된다.

 

대소 비교 연산자 <, >, <=, >=

boolean과 참조형을 제외한 나머지에 다 사용 가능한 연산자이다.

비교연산자 연산결과
> 좌변 값이 크면, true 아니면 false
< 좌변 값이 작으면 true 아니면 false
>= 좌변값이 크거나 같으면 true 아니면false
<= 좌변 값이 작거나 같으면 true 아니면 false

주의할 점은 <=와 >=의 연산자는 순서를 틀리면 안 된다는 점이다.

 

등가 비교 연산자

두 피연산자의 값이 같은지 또는 다른지를 비교하는 연산자이다.

 

비교 연산자도  
   
   

 

public class OperatorEX22 {
	public static void main(String[] args) {
		float f = 0.1f;
		double d = 0.1;
		double d2 = (double) f;

		System.out.printf("10.0==10.0f	%b%n ", 10.0 == 10.0f);
		System.out.printf("0.1==0.1f	%b%n ", 0.1 == 0.1f);
		System.out.printf("f=%19.17f%n ", f);
		System.out.printf("d =%19.17f%n ", d);
		System.out.printf("d2=%19.17f%n ", d2);
		System.out.printf("d==f %b%n ", d == f);
		System.out.printf("d==d2 %b%n ", d == d2);
		System.out.printf("d2==f %b%n ", d2 == f);
		System.out.printf("(float)d==f %b%n ", (float) d == f);
	}
}
10.0==10.0f	true
 0.1==0.1f	false
 f=0.10000000149011612
 d =0.10000000000000000
 d2=0.10000000149011612
 d==f false
 d==d2 false
 d2==f true
 (float)d==f true

이항 연산자이므로 연산을 수행하기 전에 형 변환을 통해 두 피연산자의 타입을 같게 맞춘 다음 피연산자를 비교한다.

 

문자열의 비교

두 문자열을 비교할 때는, 비교 연산자'=='대신 equals()라는 메서드를 사용해야 한다.\비교 연산자는 두 문자열이 완전히 같은 것인지 비교할 뿐이므로, 문자열의 내용이 같은지 비교하기 위해서는 equals()를 사용하는 것이다.

public class OperatorEx23_ {
	public static void main(String[] args) {
		String str1 = "abc";
		String str2 = new String("abc");
		

		System.out.printf("\"abc\"==\"abc\" ? %b%n", "abc"=="abc");
		System.out.printf(" str1==\"abc\" ? %b%n", str1=="abc");
		System.out.printf(" str1==\"abc\" ? %b%n", str2=="abc");
		System.out.printf("str1.equals(\"abc\") ? %b%n", str1.equals("abc"));
		System.out.printf("str2.equals(\"abc\") ? %b%n", str2.equals("abc"));
		System.out.printf("str2.equalsIgnotreCase(\"abc\") ? %b%n", str2.equalsIgnoreCase("abc"));
	}
}
"abc"=="abc" ? true
 str1=="abc" ? true
 str1=="abc" ? false
str1.equals("abc") ? true
str2.equals("abc") ? true
str2.equalsIgnotreCase("abc") ? true

str2와 'abc'의 내용이 같아도 false를 얻는다. 내용이 같지만 서로 다른 객체라서 그렇다.

 

산술 연산자

사칙 연산자는 덧셈(+) 뺄셈(-) 곱셈(*) 나눗셈(/)이다.

주의할 점은 나누기 연산자를 할 때 피연산자가 둘 다 int타입인 경우 소수점을 저장하지 못하므로 정수만 남고 소수점 이하는 버려지며, 반올림도 발생하지 않는다.

public class OperatorEx05 {
	public static void main(String[] args) {
		int a = 10;
		int b = 4;
        
		System.out.printf("%d + %d = %d%n", a, b, a + b);
		System.out.printf("%d - %d = %d%n", a, b, a - b);
		System.out.printf("%d * %d = %d%n", a, b, a * b);
		System.out.printf("%d / %d = %d%n", a, b, a / b);
		System.out.printf("%d / %f = %f%n", a, (float) b, a / (float) b);
	}
}
10 + 4 = 14
10 - 4 = 6
10 * 4 = 40
10 / 4 = 2
10 / 4.000000 = 2.500000

 

 

 

x y x/y x%y
유한수 +-0.0 +-Infinity NaN
유한수 +-Infinity +-0.0 x
+-0.0 +-0.0 NaN NaN
+-Infinity 유한수 +-Infinity NaN
+-Infinity +-Infinity NaN NaN

피연산자가 유한수가 아닐 경우 연산 결과이다.

기본적으로 올바른 연산 결과를 얻기 위해서는 수치가 큰 쪽으로 형 변환하며, 만약 크기가 작다면 int형으로 자동으로 변환되어 계산된다.

또한 기존에 변수를 byte로 정의하더라도, 산술 연산자로 연산을 하면 int로 자동 형 변환(rype conversion, casting)이 되어 계산이 된다.

그리고 큰 자료형에서 작은 자료형으로 변환하거나 논리형이 다르면 데이터 손실이 발생하므로 

같은 논리형의 동일한 크기의 자료 혹은 같은 논리형의 큰 크기의 자료형으로 변경해야 데이터 손실이 적거나 없다.

 

또한 자료형의 범위를 넘어가면 오버플로우나 언더플로우가 발생한다.

 

그리고 일반적인 사칙연산과 다르게 자바 프로그램의 사칙연산은 숫자뿐 아니라 문자로도 사칙연산이 가능하다.

이는 문자가 실제로 문자 그대로 저장되는 것이 아니라 해당 문자의 유니코드로 바뀌어 저장되기 때문이다.

만약 문자에 해당하는 숫자를 보고 싶다면 ch타입의 문자를 int형태로 형 변환하면 된다.

 

문자 코드 문자 코드 문자 코드
0 48 A 65 a 97
1 49 B 66 b 98
2 50 C 67 c 99
3 51 D 68 d 100
4 52 E 69 e 101
5 53 ... ... ... ...
6 54 X 88 x 120
7 55 Y 89 y 121
8 56 Z 90 z 122

숫자랑 문자는 연속적으로 되어있는데 특이하게 영문의 경우 대문자 소문자의 시작 코드 사이에 다른 문자가 있으니 문자 형태로 계산할 시 조심하자. 소문자 a에서 대문자 A를 빼면 32의 값이 차이가 나며 대문자, 소문자, 숫자의 시작 코드와 소문자와 대문자의 코드의 차가 32라는것을 알아두면 좋다.

public class OperatorEx12 {
	public static void main(String[] args) {
		
		char c1 = 'a';	// c1에는 문자 'a'의 코드값인 97이 저장된다.
		char c2 = c1;	// c1에 저장되어 있는 값이 c2에 저장된다.
		char c3 = ' ';	// c3를 공백으로 초기화 한다.
		
		int i = c1 + 1;	// 'a'+1 > 97+1 > 98
		
		c3 = (char)(c1 + 1);	// c1 + 1의 형태가 int 이므로 char로 변환
		c2++;
		c2++;
		
		System.out.println("i=" + i);
		System.out.println("c2=" + c2);
		System.out.println("c3=" + c3);
	}
}
i=98
c2=c
c3=b

c1+1은 int로 바꾼'a'+1=97+1=98이 되어 저장된다,

c2는 형변환 없이 c2에 에 저장된 값에 +2가 되므로 (char)99='c'가되는 것이다.

보통 일부러 뻔한 리터럴 연산을 풀어 쓸 필요가없이 답을 적으면 되지만, 가독성 때문에 그렇게 하는 경우가있다.

예를들어 86400이라는 숫자는 무엇을 의미하는지 선뜻 이해하기 어려울 수 있지만 이것을 60*60*24로 쓰면 초, 분, 시 를곱한값으로 이해하기 쉬워진다. 이렇게 써도 컴파일러에 의해 미리 계산되기 때문에 성능 차이는없다.

 

public class OperatorEx17 {
	public static void main(String[] args) {
		double pi = 3.141592;
		double shortPi = (int)(pi * 1000 + 0.5) / 1000.0;
		System.out.println(shortPi);
	
}
3.142

나눗셈의 경우 주의할 점이 있는데 double타입으로 정의한 변수를 나눈다 하더라도 위의 경우 int형으로 계산이 되며 나머지는 버려진다는 점이다. 반면에 이런 특징을 이용해서 식으로 반올림을 할 수 있다.

3강의 예제를 모아둔 것이다.

일부는 다른 글에서 설명하면서 사용할 것이고 일부는 넘어갈 거라서 만약 코드 적기가 귀찮다면 복사 붙여넣기해서 사용하자.

ctrl + F 한 후 class이름을 검색하면 쉽게 찾을 수 있다.

public class OperatorEx01 {
	public static void main(String[] args) {

		int i = 5;
		i++; // i=i+1;
		System.out.println(i);

		i = 5;
		++i;
		System.out.println(i);

	}

}
6
6

 

 

public class OperatorEx02 {
	public static void main(String[] args) {
		int i = 5, j = 0;
		j = i++;
		System.out.println("j=i++; 실행 후, i=" + i + ", j=" + j);
		i = 5; 
		j = 0;
		j = ++i;
		System.out.println("j=++i; 실행 후, i=" + i + ", j=" + j);
	}
}
j=i++; 실행 후, i=6, j=5
j=++i; 실행 후, i=6, j=6

 

 

public class OperatorEx03 {
	public static void main(String[] args) {
		int i=5, j=5;
		System.out.println(i++);
		System.out.println(++j);
		System.out.println("i = " + i + ", j = " + j);		
		int x = 5;		
		x = x++ - ++x;
		System.out.println(x);		
		byte b = -128;
		b = (byte)-b;
		System.out.println(-b);
	}
}
5
6
i = 6, j = 6
-2
128

 

 

public class OperatorEx04 {
	public static void main(String[] args) {
		int i = -10;
		i = +i;
		System.out.println(i);

		i = -10;
		i = -i;
		System.out.println(i);
	}
}
-10
10

 

 

public class OperatorEx05 {
	public static void main(String[] args) {
		int a = 10;
		int b = 4;
        
		System.out.printf("%d + %d = %d%n", a, b, a + b);
		System.out.printf("%d - %d = %d%n", a, b, a - b);
		System.out.printf("%d * %d = %d%n", a, b, a * b);
		System.out.printf("%d / %d = %d%n", a, b, a / b);
		System.out.printf("%d / %f = %f%n", a, (float) b, a / (float) b);
	}
}
10 + 4 = 14
10 - 4 = 6
10 * 4 = 40
10 / 4 = 2
10 / 4.000000 = 2.500000
66.66666666666667
66.67
66.66
66.66200

 

 

public class Exercise3_06 {
	public static void main(String[] args) {
		byte a = 10;
		byte b = 20;
		byte c = a + b;
		System.out.println(c);
	}
}
30

 

 

public class Exercise3_07 {
	public static void main(String[] args) {
		byte a = 10;
		byte b = 20;
		byte c = (byte) (a + b);
		System.out.println(c);
	}
}
44

 

 

public class OperatorEx08 {
	public static void main(String[] args) {
		int a = 1_000_000;	// 1,000,000 1백만
		int b = 2_000_000;  // 2,000,000 2백만
		
//		int c = a * b; // 2 * 10^12
		long c = a* b;	// a* b = 2,000,000,000,000 ?
		System.out.println(c);
	}

}
-1454759936

 

public class OperatorEx09 {
	public static void main(String[] args) {
		long a = 1_000_000 * 1_000_000;
		long b = 1_000_000 * 1_000_000L;
		
		System.out.println("a="+a);
		System.out.println("b="+b);
	}

}
a=-727379968
b=1000000000000

 

 

public class OperatorEx10 {
	public static void main(String[] args) {
		int a = 1000_000;

		int result1 = a * a / a;
		int result2 = a / a * a;
		
		System.out.printf("%d * %d / %d = %d%n", a, a, a, result1);
		System.out.printf("%d / %d * %d = %d%n", a, a, a, result2);

	}
}
1000000 * 1000000 / 1000000 = -727
1000000 / 1000000 * 1000000 = 1000000

 

 

public class OperatorEx11 {
	public static void main(String[] args) {
		char a = 'a';
		char d = 'd';
		char zero = '0';
		char two = '2';
		
		System.out.printf("'%c' - '%c' = %d%n", d, a, d - a);
		System.out.printf("'%c' - '%c' = %d%n", two, zero, two - zero);
		System.out.printf("'%c' = %d%n", a, (int)a);
		System.out.printf("'%c' = %d%n", d, (int)d);
		System.out.printf("'%c' = %d%n", zero, (int)zero);
		System.out.printf("'%c' = %d%n", two, (int)two);
		
//		char c1 = 'A';
//		char c2 = c1 + 1; // 변수와 리터럴의 연산이라 안됨 final로 리터럴로바꾸면 ㄱ나ㅡㅇ
//		상수 포함
		char c2 = 'a' + 1; // 리터럴 간의 연산
		
		char c3 = 0xAC00;
		System.out.println(c2);
		System.out.println(c3);
		
		int sec = 60 * 60 * 24; // 
		System.out.println(sec);
	}

}
'd' - 'a' = 3
'2' - '0' = 2
'a' = 97
'd' = 100
'0' = 48
'2' = 50
b
가
86400

 

 

public class OperatorEx12 {
	public static void main(String[] args) {
		
		char c1 = 'a';	// c1에는 문자 'a'의 코드값인 97이 저장된다.
		char c2 = c1;	// c1에 저장되어 있는 값이 c2에 저장된다.
		char c3 = ' ';	// c3를 공백으로 초기화 한다.
		
		int i = c1 + 1;	// 'a'+1 > 97+1 > 98
		
		c3 = (char)(c1 + 1);	// c1 + 1의 형태가 int 이므로 char로 변환
		c2++;
		c2++;
		
		System.out.println("i=" + i);
		System.out.println("c2=" + c2);
		System.out.println("c3=" + c3);
	}

}
i=98
c2=c
c3=b

 

 

public class OperatorEx13 {
	public static void main(String[] args) {
		char c1 = 'a';
		
//		char c2 = c1 + 1; // 컴파일 에러
		char c2 = 'a' + 1;	//리터럴간의 연산이므로 에러 X
		
		System.out.println(c1);
		System.out.println(c2);
	}

}
a
b

 

 

public class OperatorEx14 {
	public static void main(String[] args) {
		char c = 'a';
		for (int i = 0; i < 26; i++) {
			// 블럭{} 안의 문장을 26번 반복한다.]
			System.out.print(c++);
		} // 'a;부터 26개의 문자를 출력한다.

		System.out.println(); // 줄바꿈을한다

		c = 'A';
		for (int i = 0; i < 26; i++) { // 블럭 {} 안의 문장을 26번 반복한다.
			System.out.print(c++);	   //'A'부터 26개의 문자를 출력한다.
		}

		System.out.println();

		c = '0';
		for (int i = 0; i < 10; i++)	// 블럭{} 안의 문장을 10번 반복한다.
			System.out.println(c++);	// '0'부터 10개의 문자를 출력한다.
		System.out.println();

		// 줄바꿈을한다
	}
}
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0
1
2
3
4
5
6
7
8
9

 

 

import java.util.Scanner;

public class OperatorEx15 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		char lowerCase = 'x';
		char upperCase = (char)(lowerCase - 32);
		System.out.println(upperCase);
		}
 }
X

 

 

public class OperatorEx16 {
	public static void main(String[] args) {
		float pi = 3.141592f;
		float shortPi = (int)(pi * 1000) / 1000f;
		System.out.println(shortPi);
	}

}
3.141

 

 

public class OperatorEx17 {
	public static void main(String[] args) {
		double pi = 3.141592;
		double shortPi = (int)(pi * 1000 + 0.5) / 1000.0;

		System.out.println(shortPi);
	}

}
3.142

 

 

public class OperatorEx18 {
	public static void main(String[] args) {
		double pi = 3.141592;
		double shortPi = Math.round(pi * 1000) / 1000.0;
		System.out.println(shortPi);
	}

}
3.142

 

 

public class OperatorEx19 {
	public static void main(String[] args) {
		int x = 10;
		int y = 8;
		
		System.out.printf("%d을 %d로 나누면, %n", x, y);
		System.out.printf("몫은 %d이고, 나머지는 %d입니다.%n", x / y, x % y);
        	}

}
10을 8로 나누면, 
몫은 1이고, 나머지는 2입니다.

 

 

public class OperatorEx20 {
	public static void main(String[] args) {
		System.out.println(-10%8);
		System.out.println(10%-8);	
		System.out.println(-10%-8);		
	}

}
-2
2
-2

 

 

public class OperatorEx21 {
	public static void main(String[] args) {
		System.out.printf("10 == 10.0f \t %b%n", 10==10.0f);
		System.out.printf("'0' == 0 \t %b%n", '0' == 0);	
		System.out.printf("'A' == 65 \t %b%n", 'A' == 65);
		System.out.printf("'A' > 'B' \t %b%n", 'A' > 'B');	 
		System.out.printf("'A'+1 != 'B' \t %b%n", 'A'+1 != 'B');	
	}
}
10 == 10.0f 	 true
'0' == 0 	 false
'A' == 65 	 true
'A' > 'B' 	 false
'A'+1 != 'B' 	 false

 

 

public class OperatorEX22 {
	public static void main(String[] args) {
		float f = 0.1f;
		double d = 0.1;
		double d2 = (double) f;

		System.out.printf("10.0==10.0f	%b%n ", 10.0 == 10.0f);
		System.out.printf("0.1==0.1f	%b%n ", 0.1 == 0.1f);
		System.out.printf("f=%19.17f%n ", f);
		System.out.printf("d =%19.17f%n ", d);
		System.out.printf("d2=%19.17f%n ", d2);
		System.out.printf("d==f %b%n ", d == f);
		System.out.printf("d==d2 %b%n ", d == d2);
		System.out.printf("d2==f %b%n ", d2 == f);
		System.out.printf("(float)d==f %b%n ", (float) d == f);
	}

}
0.1==0.1f	false
 f=0.10000000149011612
 d =0.10000000000000000
 d2=0.10000000149011612
 d==f false
 d==d2 false
 d2==f true
 (float)d==f true

 

 

public class OperatorEx23 {
	public static void main(String[] args) {
		String str1 = "abc";
		String str2 = new String("abc");

		System.out.printf("\"abc\"==\"abc\" ? %b%n", "abc"=="abc");
		System.out.printf(" str1==\"abc\" ? %b%n", str1=="abc");
		System.out.printf("str1.equals(\"abc\") ? %b%n", str1.equals("abc"));
		System.out.printf("str2.equals(\"abc\") ? %b%n", str2.equals("abc"));
		System.out.printf("str2.equalsIgnotreCase(\"abc\") ? %b%n", str2.equalsIgnoreCase("abc"));
	}

}
"abc"=="abc" ? true
 str1=="abc" ? true
 str1=="abc" ? false
str1.equals("abc") ? true
str2.equals("abc") ? true
str2.equalsIgnotreCase("abc") ? true

 

 

public class OperatorEx24 {
	public static void main(String[] args) {
		int x = 0;
		char ch = ' ';
		
		x = 15;
		System.out.printf("x=%2d, 10 < x && x < 20 = %b%n", x, 10 < x && x < 20);
		
		x = 6;
		System.out.printf("x=%2d , x%%2==0 || x%%3==0 && x%%6!=0 = %b%n",x, x%2==0 || x%3==0&&x%6!=0);
		System.out.printf("x=%2d , (x%%2==0 || x%%3==0) && x%%6!=0 = %b%n",x, (x%2==0 || x%3==0)&&x%6!=0);
		
		ch='1';
		System.out.printf("ch='%c', '0' <= ch && ch <= '9' =%b%n", ch, '0' <= ch && ch <='9');
		
		ch='a';
		System.out.printf("ch='%c', 'a' <= ch && ch <= 'z' =%b%n", ch, 'a' <= ch && ch <='z');
		
		ch='A';
		System.out.printf("ch='%c', 'A' <= ch && ch <= 'Z' =%b%n", ch, 'A' <= ch && ch <='Z');
		

		ch='q';
		System.out.printf("ch='%c', 'q' <= ch && ch <= 'Q' =%b%n", ch, 'q' <= ch && ch <='Q');
	}

}
x=15, 10 < x && x < 20 = true
x= 6 , x%2==0 || x%3==0 && x%6!=0 = true
x= 6 , (x%2==0 || x%3==0) && x%6!=0 = false
ch='1', '0' <= ch && ch <= '9' =true
ch='a', 'a' <= ch && ch <= 'z' =true
ch='A', 'A' <= ch && ch <= 'Z' =true
ch='q', 'q' <= ch && ch <= 'Q' =false

 

 

import java.util.Scanner;

public class OperatorEx25 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		char ch = ' ';
		for (int i = 1; i <= 10; i--) {

			System.out.printf("문자를 하나 입력하세요.>");

			String input = scanner.nextLine();
			ch = input.charAt(0);

			if ('0' <= ch && ch <= '9') {
				System.out.printf("입력하신 문자는 숫자입니다.%n");
			}
			if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'z')) {
				System.out.printf("입력하신 문자는 영문자입니다.%n");
			}	
		} // main
		scanner.close();}
문자를 하나 입력하세요.>a
입력하신 문자는 영문자입니다.
문자를 하나 입력하세요.>1
입력하신 문자는 숫자입니다.
문자를 하나 입력하세요.>

 

 

public class OperatorEx26 {
	public static void main(String[] args) {
		int a = 5;
		int b = 0;

		System.out.printf("a=%d, b=%d%n", a, b);
		System.out.printf("a!=0 || ++b!=0 = %b%n", a != 0 || ++b != 0);
		System.out.printf("a=%d, b=%d%n", a, b);
		System.out.printf("a==0 && ++b!=0 = %b%n", a == 0 && ++b != 0);
		System.out.printf("a=%d, b=%d%n", a, b);
	}
}
a=5, b=0
a!=0 || ++b!=0 = true
a=5, b=0
a==0 && ++b!=0 = false
a=5, b=0

 

 

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);
		System.out.printf("!!b=%b%n", !!b);
		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');
		} // main의 끝
}
b=true
!b=false
!!b=true
!!!b=false

ch=c
ch < 'a' || ch > 'z'=false
!('a'<=ch && ch<='z'=false
'a'<=ch && ch<='z' =true
4
7
3
-2
24
1
11111111111111111111111111111010
11111111111111111111111111111101
1111111111111111111111111111101
2147483645
5
5

 

 

public class OperatorEx28 {
	public static void main(String[] args) {
		int x = 0xAB, y = 0xF;

		System.out.printf("x = %#X  \t\t%s%n", x, toBinaryString(x));
		System.out.printf("y = %#X  \t\t%s%n", y, toBinaryString(y));
		System.out.printf("%#X | %#X = %#X \t%s%n", x, y, x | y, toBinaryString(x | y));
		System.out.printf("%#X & %#X = %#X \t%s%n", x, y, x & y, toBinaryString(x & y));
		System.out.printf("%#X ^ %#X = %#X \t%s%n", x, y, x ^ y, toBinaryString(x ^ y));
		System.out.printf("%#X ^ %#X ^ %#X = %#X \t%s%n", x, y, y, x ^ y, toBinaryString(x ^ y ^ y));

	} // main의 끝

	static String toBinaryString(int x) { // 10진 정수를 2진수로 변환하는 메서드
		String zero = "00000000000000000000000000000000";
		String tmp = zero + Integer.toBinaryString(x);
		return tmp.substring(tmp.length() - 32);
	}
}
x = 0XAB  		00000000000000000000000010101011
y = 0XF  		00000000000000000000000000001111
0XAB | 0XF = 0XAF 	00000000000000000000000010101111
0XAB & 0XF = 0XB 	00000000000000000000000000001011
0XAB ^ 0XF = 0XA4 	00000000000000000000000010100100
0XAB ^ 0XF ^ 0XF = 0XA4 	00000000000000000000000010101011

 

 

public class OperatorEx29 {
	public static void main(String[] args) {
		byte p = 10;
		byte n = -10;

		System.out.printf(" p =%d |t%s%n", p, toBinaryString(p));
		System.out.printf("~p =%d |t%s%n", ~p, toBinaryString(~p));
		System.out.printf("~p+1 =%d |t%s%n", ~p+1, toBinaryString(~p+1));
		System.out.printf("~~p =%d |t%s%n", ~~p, toBinaryString(~~p));
		System.out.println();
		System.out.printf(" n =%d%n", n);
		System.out.printf("~(n-1)=%d%n", ~(n-1));
	} // main의 끝

	static String toBinaryString(int x) { // 10진 정수를 2진수로 변환하는 메서드
		String zero = "00000000000000000000000000000000";
		String tmp = zero + Integer.toBinaryString(x);
		return tmp.substring(tmp.length() - 32);
	}
}
 p =10 |t00000000000000000000000000001010
~p =-11 |t11111111111111111111111111110101
~p+1 =-10 |t11111111111111111111111111110110
~~p =10 |t00000000000000000000000000001010

 n =-10
~(n-1)=10

 

 

public class OperatorEx30 {
	// 10진 정수를 2진수로 변환하는 메서드
	static String toBinaryString(int x) { 
		String zero = "00000000000000000000000000000000";
		String tmp = zero + Integer.toBinaryString(x);
		return tmp.substring(tmp.length() - 32);
	}
	public static void main(String[] args) {
		
		int dec = 8;
		System.out.printf("%d >> %d = %4d |t%s%n", dec, 0, dec >> 0, toBinaryString(dec >> 0));
		System.out.printf("%d >> %d = %4d |t%s%n", dec, 1, dec >> 1, toBinaryString(dec >> 1));
		System.out.printf("%d >> %d = %4d |t%s%n", dec, 2, dec >> 2, toBinaryString(dec >> 2));
		System.out.printf("%d << %d = %4d |t%s%n", dec, 0, dec << 0, toBinaryString(dec << 0));
		System.out.printf("%d << %d = %4d |t%s%n", dec, 1, dec << 1, toBinaryString(dec << 1));
		System.out.printf("%d << %d = %4d |t%s%n", dec, 2, dec << 2, toBinaryString(dec << 2));
		System.out.println();
		
		dec = -8;
		System.out.printf("%d >> %d = %4d |t%s%n", dec, 0, dec >> 0, toBinaryString(dec >> 0));
		System.out.printf("%d >> %d = %4d |t%s%n", dec, 1, dec >> 1, toBinaryString(dec >> 1));
		System.out.printf("%d >> %d = %4d |t%s%n", dec, 2, dec >> 2, toBinaryString(dec >> 2));
		System.out.printf("%d << %d = %4d |t%s%n", dec, 0, dec << 0, toBinaryString(dec << 0));
		System.out.printf("%d << %d = %4d |t%s%n", dec, 1, dec << 1, toBinaryString(dec << 1));
		System.out.printf("%d << %d = %4d |t%s%n", dec, 2, dec << 2, toBinaryString(dec << 2));
		System.out.println();
		
		dec = 8;
		System.out.printf("%d >> %2d = %4d |t%s%n", dec, 0, dec >> 0, toBinaryString(dec >> 0));
		System.out.printf("%d >> %2d = %4d |t%s%n", dec, 32, dec >> 32, toBinaryString(dec >> 32));
		
	} // main의 끝
}
8 >> 0 =    8 |t00000000000000000000000000001000
8 >> 1 =    4 |t00000000000000000000000000000100
8 >> 2 =    2 |t00000000000000000000000000000010
8 << 0 =    8 |t00000000000000000000000000001000
8 << 1 =   16 |t00000000000000000000000000010000
8 << 2 =   32 |t00000000000000000000000000100000

-8 >> 0 =   -8 |t11111111111111111111111111111000
-8 >> 1 =   -4 |t11111111111111111111111111111100
-8 >> 2 =   -2 |t11111111111111111111111111111110
-8 << 0 =   -8 |t11111111111111111111111111111000
-8 << 1 =  -16 |t11111111111111111111111111110000
-8 << 2 =  -32 |t11111111111111111111111111100000

8 >>  0 =    8 |t00000000000000000000000000001000
8 >> 32 =    8 |t00000000000000000000000000001000

 

 

public class OperatorEx31 {
	public static void main(String[] args) {
		int dec =1234;
		int hex = 0xABCD;
		int mask = 0xF;
		
		System.out.printf("hex=%X%n", hex);
		System.out.printf("%X%n", hex & mask);
		
		hex = hex >> 4;
		System.out.printf("%X%n", hex & mask);
		
		hex = hex >> 4;
		System.out.printf("%X%n", hex & mask);
		
		hex = hex >> 4;
		System.out.printf("%X%n", hex & mask);
	}

}
hex=ABCD
D
C
B
A

 

 

public class OperatorEx32 {
	public static void main(String[] args) {
		int x, y, z;
		int absX, absY, absZ;
		char signX, signY, signZ;
		
		x = 10;
		y = -5;
		z = 0;
		
		absX = x >= 0 ? x : -x; // x의 값이 음수이면, 양수로 만든다.
		absY = y >= 0 ? y : -y;
		absZ = z >= 0 ? z : -z;
		
		signX = x > 0 ? '+' : (x==0 ? ' ' : '-'); //조건 연산자를 중첩
		signY = y > 0 ? '+' : (y==0 ? ' ' : '-');
		signZ = z > 0 ? '+' : (z==0 ? ' ' : '-'); 
		
		System.out.printf("x=%c%d%n", signX, absX);
		System.out.printf("y=%c%d%n", signY, absY);
		System.out.printf("z=%c%d%n", signZ, absZ);
		
	}

}
x=+10
y=-5
z= 0

 

+ Recent posts