728x90
반응형
안녕하세요.
오늘은 연산자(Operator)에 관하여 적어보겠습니다.
연산, 연산자, 피 연산자, 연산식
- 프로그램에서의 데이터를 처리하여 결과를 산출하는 것을 연산(operations)이라고 합니다.
- 연산에 사용되는 표시나 기호를 연산자(operator)라고 합니다.
- 연산에 참여하는 변수나 상수를 피 연산자(operand)라고 합니다.
- 연산자와 피연산자를 이용해 연산의 과정을 기술한 것을 연산식(expressions)이라고 합니다.
1. 단향 연산자(피 연산자 1개)
(1)부호 연산자
+(양수),-(음수) 표시
//단항연산자(부호연산자)
int x = 100;
int result1= +x;
int result2= -x;
System.out.println("result1=" + result1);
System.out.println("result2=" + result2);
/*
결과
result1=100
result2=-100
*/
(2)증감 연산자(++,--)
변수의 값이 1증가 하거나 1감소하는 연산자 이다.
//증감연산자(++, --)
int a=10, b=10, c=0;
c = +a--;
System.out.println("a=" + a);
System.out.println("c=" + c);
/*
결과
a=9
c=10
*/
(3)논리 부정 연산자(!)
true->false, false->true 변경하는 변수이기에 boolean타입에만 사용 가능하다.
//부정연산자(!)
boolean play=true;
play=!play;
System.out.println("play=" + play);
play=!play;
System.out.println("play=" + play);
/*
결과
play=false
play=true
*/
2. 이향 연산자(피 연산자 2개)
(1)산술 연사자(+,-,*,/,%)
//산술연산자(+ -, *, /, %)
int x1=10, y1=3, z1=0;
z1 = x1 % y1;
System.out.println(x1 + "%" + y1 + "=" + z1);
/*
결과:
10%3=1
*/
(2)문자열 연결 연산자(+)
//문자열 연결 연산자(+)
String name="홍길동";
String str = "저는'" + name + "'입니다.";
System.out.println(str);
/*
저는'홍길동'입니다.
*/
(3)비교 연산자(< , <=, >, >=, ==, !=)
구분 | 연산식 | 설명 | ||
동등비교 | 피연산자1 | == | 피연산자2 | 두 피연산자의 값이 같은지를 검사 |
피연산자1 | != | 피연산자2 | 두 피연산자의 값이 다른지를 검사 | |
크기비교 | 피연산자1 | > | 피연산자2 | 피연산자1이 큰지를 검사 |
피연산자1 | >= | 피연산자2 | 피연산자1이 크거나 같은지를 검사 | |
피연산자1 | < | 피연산자2 | 피연산자1이 작은지를 검사 | |
피연산자1 | <= | 피연산자2 | 피연산자1이 작거나 같은지를 검사 |
//비교연산자(<, <=, >, >=, ==, !=)
int x2=10, y2=10;
boolean result;
result= x2 < y2;
System.out.println("결과:" + result);
result= x2 <= y2;
System.out.println("결과:" + result);
result= x2 > y2;
System.out.println("결과:" + result);
result= x2 >= y2;
System.out.println("결과:" + result);
result= x2 == y2;
System.out.println("결과:" + result);
result= x2 != y2;
System.out.println("결과:" + result);
/*
결과:
결과:false
결과:true
결과:false
결과:true
결과:true
결과:false
*/
(4) 논리 연산자(&&, ||, !)
&&: AND
||: OR
!: NOT
//논리연산자(&&, ||, !)
result = (x2 > 10) && (y2 == 10);
System.out.println("결과:" + result);
result = (x2 > 10) || (y2 == 10);
System.out.println("결과:" + result);
result = !((x2 > 10) || (y2 > 10));
System.out.println("결과:" + result);
/*
결과:
결과:false
결과:true
결과:true
*/
(5) 대입 연산자 (=, +=, -=, *=, /=. %=)
=는 같다는 뜻이 아닌 int x3에 10을 대입한다는 의미이다.
//대입연산자(=, +=, -=)
int x3=10;
int result3 =10;
result3 += 5; //result3 = result3 + 5
System.out.println("result3=" + result3);
result3 -=3; // result3 = result3 - 3
System.out.println("result3=" + result3);
/*
결과:
result3=15
result3=12
*/
3.삼향 연산자(피 연산자 3개)
조건식을 아래와 같이 진행 할 경우 true일 경우 "양수" 이며 false일 경우 "음수" 이기에 10은 0보다 크기에 true가 나오며 양수 결과가 나오는 것을 확인 할 수 있다.
조건식(피 연산자1)? 값or연산식(피 연산자2) : 값or연산식(피 연산자3)
//삼항연산자
int x3=10;
String result4;
result4 = x3 > 0 ? "양수": "음수";
System.out.println("result4=" + result4);
/*
결과:
result4=양수
*/
부족한 글 읽어주셔서 감사합니다.
아직 배우고 있는 과정이기 잘못되거나 수정할 부분 있으면 알려주시면 수정반영하겠습니다.
감사합니다.
728x90
반응형
'👨💻개발 > Language' 카테고리의 다른 글
[Java]Oracle DB Java에 연동하기 (2) | 2021.02.07 |
---|---|
[Java]형변환(자동 타입 변환(Promotion), 강제 타입 변환(Casting)) (0) | 2021.02.03 |
[Java]Scanner class (0) | 2021.02.02 |
[Java] 변수(Variable)와 데이터 타입(Data type) (0) | 2021.01.27 |
[Java]Eclipse Workspace 설정 / 다크모드, 폰트설정 / Java Project 생성 / Class 생성 / 출력문 (0) | 2021.01.18 |