연산자 : 연산에 사용되는 표시나 기호를 연산자라고 함
피연산자 : 연산자와 함꼐 연산되는 데이터
연산식 : 연산자와 피연산자를 이용하여 연산의 과정을 기술한 것
ex) +, -, *, ==은 연산자고, x,y,z 변수는 피연산자
종류 연산자 기능 우선순위
증감 연산자 ++, -- 1만큼 증가 감소 1순위
산술 연산자 +, -, *, /, % 사칙연산 및 나머지 계산 2순위
시프트 연산자 >>, <<, >>> bit 값을변화를 일으키는 연산자 3순위
비교 연산자 >, <. >=, <=, ==, != 변수나 상수의 값을 비교할때 사용 4순위
비트 연산자 &, |, ^, ~ 피연산자의 값들을 bit단위로 변경후 연산수행 ~만 1순위, 나머지는 5순위
논리 연산자 !, &, |, &&, || true나 fase인 값을 가지고 다시 연산하는 연산자 !만 1순위, 나머지는 6순위
조건(삼항) 연산자 (조건식)? A : B 조건식에 따라 A 또는 B 중 하나를 선택 7순위
대입 연산자 =, *=, /=, %=, +=, -= 우변의 값을 좌변의 변수에 대입 8순위
ex)
package ch03;
public class Ch03_1 {
public static void main(String[] args) {
int x = 10;
int y = 20;
boolean result = x + y > 10 && x + y < 20;
// 10 + 20 > 10 && 10 + 20 < 20;
// 30 > 10 && 30 < 20;
// T && F
// false
System.out.println(result);
}
}
단항 연산자
피연산자가 단 하나뿐인 연산자, 부호연산자(+,-) 증감연산자(++,--) 논리 부정 연산자(!)가 있다.
부호연산자(+,-) : boolean타입과 char타입을 제외한 나머지 기본 타입에 사용할 수 있다.
(++ 또는 --)피연산자 : 다른 연산을 수행하기 전에 피연산자의 값을 1증가 또는 감소 시킴
피연산자(++ 또는 --) : 다른 연산을 수행한 후에 피연산자의 값을 1 증가 또는 감소 시킴
피연산자가 2개인 연산자를 말하며, 여기에는 산술 연산자, 문자열 결합, 비교 연산자, 논리 연산자, 대입 연산자 등이 있습니다.
package ch03;
public class Ch03_5_ARithmeticOperatorExample {
public static void main(String[] args) {
int v1 = 5;
int v2 = 2;
int result1 = v1 + v2;
System.out.println("result1 = " + result1);
int result2 = v1 - v2;
System.out.println("result2 = " + result2);
int result3 = v1 * v2;
System.out.println("result3 = " + result3);
int result4 = v1 / v2;
System.out.println("result4 = " + result4);
int result5 = v1 % v2;
System.out.println("result5 = " + result5);
double result6 = (double) v1 / v2;
System.out.println("result6 = " + result6);
}
}
리터럴같의 연산은 컴파일 단계에서 수행하기 때문에 타입 변환이 없다.
package ch03;
public class Ch03_6_CharOperationExample {
public static void main(String[] args) {
char c1 = 'A' + 1;
char c2 = 'A';
char c3 = (char)(c2 + 1);
//char 변수가 산술 연산에 사용되면 int 타입으로 변환되므로 연산결과는 int 타입이 됨.
//리터럴같의 연산은 컴파일 단계에서 수행하기 때문에 타입 변환이 없다.
System.out.println("c1: " + c1);
System.out.println("c2: " + c2);
System.out.println("c3: " + c3);
}
}
package ch03;
public class StringConcatExample {
public static void main(String[] args) {
// String + double(숫자)
// 문자열 + 숫자
// JDK6.0
String str1 = "JDK" + 6.0;
// JDK6.0 + "특징"
// 문자열 + 문자열
// "JDK6.0" + "특징"
// JDK6.0 특징
String str2 = str1 + " 특징";
System.out.println(str2);
// 문자열 + 숫자
// "JDK3"
// "JDK3" + 3.0
// "JDK33.0"
String str3 = "JDK" + 3 + 3.0;
// 숫자 + 숫자
// 6.0
// 6.0 + "JDK"
// 6.0JDK
String str4 = 3 + 3.0 + "JDK";
System.out.println(str3);
System.out.println(str4);
}
}
< <= > >= == !=
!=랑 =!는 다름 주의해서 사용. // = ! : 논리 부정연산자
package ch03;
public class ch03_7_CompareOperator {
public static void main(String[] args) {
int num1 = 10;
int num2 = 10;
boolean result1 = (num1 == num2);
boolean result2 = (num1 != num2);
boolean result3 = (num1 <= num2);
System.out.println("result1=" + result1);
System.out.println("result2=" + result2);
System.out.println("result3=" + result3);
char char1 = 'A';
char char2 = 'B'; // 65 66
boolean result4 = (char1 < char2);
// String 타입이면 비교가 안됨 .equals 사용해서 비교
System.out.println("result4=" +result4);
String str1 ="A";
String str2 ="B";
boolean result5 = str1 == str2;
boolean result6 = str1.equals(str2);
}
}
package ch03;
public class Ch03_8_CompareOperatorExample2 {
public static void main(String[] args) {
int v2 = 1;
double v3 = 1.0;
// int(4) double(8)
// double double
// 1.0 == 1.0
System.out.println(v2==v3); //true
double v4 = 0.1;
float v5 = 0.1f;
// double(8) float(4)
// double == double
System.out.printf("%15.14f\n", v4);
System.out.printf("%15.14f\n", v5); //float이 double의 자리수를 따라가려다 이상한 값이 들어감
System.out.println((float)v4 == v5);
// double(8) float(4)
// float(4) float(4)
System.out.println((float)v4 == v5);
}
}
논리곱(&,&&) - AND : &가 하나면 전체 다 확인
논리합(|,||) - OR
배타적 논리합(^) - XOR
논리 부정(!) - NOT
package ch03;
public class ch03_9 {
public static void main(String[] args) {
int charCode= 'A';
if((charCode>=65) & (charCode<=90)) {
System.out.println("대문자군요");
}
if((charCode>=97) && (charCode<=122)) {
System.out.println("소문자군요");
}
if(!(charCode<48) && !(charCode>57)) {
System.out.println("0~9 숫자군요");
}
int value = 6;
if((value%2==0) | (value%3==0)) {
System.out.println("2 또는 3의 배수군요");
}
if((value%2==0) || (value%3==0)) {
System.out.println("2 또는 3의 배수군요");
}
}
}
대입연산자는 오른쪽 피연산자의 값을 왼쪽 피연산자인 변수에 저장합니다.
ex)
package ch03;
public class Ch03_10_AssignmentOperatorExample {
public static void main(String[] args) {
int result = 0;
result += 10;
System.out.println("result=" + result);
result -= 5;
System.out.println("result=" + result);
result *= 5;
System.out.println("result=" + result);
result /= 5;
System.out.println("result=" + result);
result %= 3;
System.out.println("result=" + result);
}
}
3개의 피연산자를 필요로 하는 연산자
?앞의 조건식에 따라 콜론(:) 앞뒤의 피연산자가 선택된다고 해서 조건 연산식이라고 부르기도 한다.
조건식 ? 값 또는 연산식 : 값 또는 연산식
(피연산자1) (피연산자2) (피연산자3)
package ch03;
public class Ch03_10 {
public static void main(String[] args) {
int score = 70;
char grade = (score > 90) ? 'A' : ((score > 80) ? 'B' : 'C');
System.out.println(score + "점은 " + grade + "등급입니다.");
}
}
Java 연산자 #2에 이어서...