목차
1) 산술연산
2) 논리연산
3) 문자열 연결 연산
1. 산술연산
package PACK01;
public class Hello01 {
public static void main(String[] args) {
/* 산술 연산자
+
-
*
/ : 몫
% : 나머지
곱셈 먼저
괄호가 제일 먼저
괄호 만드는 습관 가지기
*/
System.out.println("호랑이");
System.out.println(17+4);//21
System.out.println(17-4);//13
System.out.println(17*4);//68
System.out.println(17/4);//4
System.out.println(17%4);//1
System.out.println(3+2*4);//11
System.out.println((3+2)*4);//20
System.out.println(3+(2*4));//11
}
}
int a = 10;
System.out.println(a);
a = a+3;
System.out.println(a);
a++;
System.out.println(a); //14 //현재 값에 1 증가
a--;
System.out.println(a);//13 //현재 값에 1 감소
a = a+1;
System.out.println(a); //14 //a++과 같지만 ++이 더 빠르다.
a = a+3;
System.out.println(a); //17
a += 3;
System.out.println(a); //20 //번역이 빠르다. 이렇게 쓰는 것이 더 좋다.
부호 연산자
package PACK01;
import java.util.Random;
import java.util.Scanner;
public class Hello01 {
public static void main(String[] args) {
/* 부호연산자 */
int a1 = 10;
int b1 = 10;
int c1 = +10;
System.out.println(a1); //10
System.out.println(b1); //10
System.out.println(c1); //10
/*출력
10
10
10 */
}
}
++i와 i++의 차이
package PACK01;
import java.util.Random;
import java.util.Scanner;
public class Hello01 {
public static void main(String[] args) {
/* ++i와 i++의 차이 */
//대입 증가
int a = 10, b;
b = a++;
System.out.println(a+" "+b); //11 10
//증가 대입
int c = 10, d;
d = ++c;
System.out.println(c+" "+d); //11 11
//++
int e= 10, f = 10;
e++;
++f; // 전문가들은 이것을 선호, 통합용으로 좀 더 빠르다.
System.out.println(e+" "+f);//11 11
}
}
2. 논리 연산자
package Pack;
public class Hello01 {
public static void main(String[] args) {
/* 논리 연산자
* 논리 OR: 하나만 true이면 연산 결과는 true
* 논리 AND: 모두 ture이면 연산 결과는 true
논리 연산자 (&&, ||, &, |, ^, !) */
int key = 0;
int money = 4999;
System.out.println(key >= 0 || money >= 5000); //true
System.out.println(key >= 1 || money >= 5000); //false
//논리 OR 연산
System.out.println(false || false); //false
System.out.println(true || false); //true
System.out.println(false || true); //true
System.out.println(true || true); //true
//논리 AND 연산
System.out.println(false && false); //false
System.out.println(true && false); //false
System.out.println(false && true); //false
System.out.println(true && true); //true
// 논리 부정 ! : 연산 반대로 결과가 나온다.
System.out.println(!true); //false
System.out.println(!(3>2)); //false
// 범위 사이에는 논리 AND 연산을 쓴다.
int height = 185;
System.out.println(height >= 180 && height <= 195); //true
// 논리 OR을 쓰는 경우
int age = 15;
System.out.println(age<=10 || age>=70); //false
int save = 5200;
age = 72;
System.out.println(age<=10 || age>=70 && (save <= 5000)); //false
}
}
3. 문자열 연결 연산자
package PACK01;
public class Hello01 {
public static void main(String[] args) {
/*비교연산자
<,<=,>,>=,==,!= */
System.out.println(50>3);//true
System.out.println(50<3);//false
System.out.println(3>=3);//true
System.out.println(2>=3);//false
System.out.println(3<=3);//true
System.out.println(3==3);//true
System.out.println(3==4);//false
System.out.println(3!=4);//true
System.out.println(3!=3);//false
int a = 4, b = 3;
System.out.println(a>b); //true
System.out.println(a!=b); //true
System.out.println(a-3>b); //false 산술연산이 먼저 일어난다.
}
}
'프로그래밍 언어 문법 > Java' 카테고리의 다른 글
006. 제어문-for문 (0) | 2019.07.05 |
---|---|
005. 4대 제어문- if문 (0) | 2019.07.04 |
003. 연산자, 데이터 타입, 변수 선언과 사용, 비교연산자 (0) | 2019.07.04 |
002. 출력 형식 (0) | 2019.07.04 |
001. 자바를 시작하기 전 기본 준비 (0) | 2019.07.04 |