Chapter 03. 연산자
산술연산자
* - 사칙연산 : +, -, *, /, %(나머지)
* - 복합 연산자 : +=, -=, *=, /=, %=
* - 증감 연산자 : ++, --
곱하기와 나누기가 더하기 빼기보다 우선순위가 높다.
int result = 10 + 20 - (((30 * 40) / 50) % 60);
*나머지 연산
result = 10 / 3; // 3.3333....
System.out.println(result);
result = 10 % 4;
System.out.println(result);
복합연산자
변수에 저장되어있는 값에 연산을 수행할때
수행할 연산자와 대입 연산자를 경합해서 사용할 수 있다.
result =10;
result = result + 10;
System.out.println(result);
result += 10;
System.out.println(result);
result -= 10;
System.out.println(result);
result *= 10;
System.out.println(result);
result /= 10;
System.out.println(result);
result %= 10;
System.out.println(result);
result += 5 + 5; // result += 10; -> result = result + 10;
System.out.println(result);
증감연산자
증가연산자(++) : 변수의 값을 1 증가 시킨다.
감소연산자(--) : 변수의 값을 1 감소 시킨다.
(문자열 + 정수 = 문자열)
(연산이 먼저 이루어지고 해당 변수를 만났을때 값이 출력된다)
int i = 0;
++i; // 전위형 : 변수의 값을 읽어오기 전에 1 증가
i++; // 후위형 : 변수의 값을 읽어오고 1 증가
--i;
i--;
i = 0;
System.out.println("++i = " + (++i)); // 1
System.out.println("i = " + i); // 1
i = 0;
System.out.println("i++ = " + (i++)); // 0
System.out.println("i = " +i); // 1
표현범위가 더 큰 타입으로 형변환 된다.
int _int = 10;
double _double = 3.14;
double result2 = _int + _double;
int보다 작은 타입은 int로 형변환 된다.
byte _byte = 5;
short _short = 10;
int result3 = _byte + _short;
char _char1 = 'a';
char _char2 = 'b';
int result4 = _char1 + _char2;
System.out.println(result4);
오버플로우
byte b = 127;
b++;
System.out.println(b);
b--;
System.out.println(b);
// 타입을 선택할 때는 연산의 범위를 고려해야한다.
반올림, 올림, 버림
// 반올림 : Math.round(실수)
// 올림 : Math.ceil(실수)
// 버림 : Math.flood(실수)
랜덤
Random rnd = new Random();
rnd.nextInt(); // =>int범위 내의 int 가 랜덤으로 출력
rnd.nextInt(46); //=> 46보다 작은 int가 랜덤으로 출력
int rNum = rnd.nextInt(50) + 50;
System.out.println(rNum);
// 50~99까지 => (0 ~ 49) + 50
비교연산자
* >, <, >=, <=, ==, !=
* 참조타입(String)은 : .equals()메소드를 사용
*기본타입(byte,char,short,int,long,float,double,boolean)변수의 값 비교는 == 연산자를 사용
boolean b = 10 < 20;
System.out.println("10 < 20 -> " + b);
b = 10 <= 20 - 15;
System.out.println("10 <= 20 - 15 -> " + b);
// 비교연산자의 연산 결과는 boolean이다.
String str1 = "abc";
String str2 = "abc";
b = str1 == str2; //문자열의 내용이 아닌 주소를 비교한 것이다.
System.out.println("str1 == str2 -> " + b);
String str3 = new String("abc");
String str4 = new String("abc");
b = str3 == str4;
System.out.println("str3 == str4 -> " + b);
// String의 내용을 비교하기 위해서는 equals() 매서드를 사용한다.
b = str3.equals(str4);
System.out.println("str3.equals(str4) -> " + b);
논리연산자
*&&(AND), || (shift + \)(OR), !(NOT), ^(XOR)
boolean b = 5 < 10 && (15 % 2 == 0 || 9 >= 5);
System.out.println("5 < 10 && (15 % 2 == 0 || 9 >= 5) -> " + b);
// ||가 먼저 계산되는 의도를 반영하기 위해서는
// 반드시 괄호를 넣어주어야 한다.
// 효율적인 연산
b = true && true; // true
b = true && false; // false
b = false && true; // false
b = false && false; // false
b = true || true; // true
b = true || false; // true
b = false || true; // true
b = false || false; // false
// 왼쪽의 피연산자에서 결과가 정해지면 오른쪽은 수행하지 않는다
비트연산자
*http://tcpschool.com/java/java_operator_bitwise
* - |, &, ^, ~, <<, >>
* - 비트단위로 연산한다.
기타 연산자
* .(참조연산자) : 특정 범위 내에 속해있는 멤버를 지칭할 때 사용한다.
* (type) : 형변환
삼항연산자
* -조건식 ? (조건식이 참일 경우 수행할 문장)
: (조건식이 거짓일 경우 수행할 문장)
([조건] 조건 ? 참일때 값 : 거짓일때 값
[공식] 조건 ? 값 : 값)
//수학점수가 90점 이상이면 'A'
//80점 이상이면 'B'
//80점 미만이면 'C'
System.out.println(math >= 90 ? "A" :
math >=80 ? "B" : "C");