Binary World

JAVA CLASS : 연산자 본문

개발자의 길/JAVA

JAVA CLASS : 연산자

모쿠 2016. 12. 29. 11:30

<JAVA Practice>


* OperatorMain01(자바의 계산)


public class OpMain01 {


public static void main(String[] args) {

System.out.println("[1] 대입 연산자(=)");

// 변수 = 값;

// 오른쪽의 값을 왼쪽의 변수에 저장하는 연산자

int number = 123;

System.out.println("number = " + number);

System.out.println("===============");

System.out.println("[2] 산술 연산자(+, -, *, /, %)");

// (정수) / (정수) : 나눈 몫을 계산

// (정수) % (정수) : 나눈 나머지르르 계산

// (실수) / (실수), (실수) / (정수), (정수) / (실수) : 

// 소수점까지 계산하는 나눗셈 결과

System.out.println("정수 나눗셈 몫: " + (123 / 100));

System.out.println("정수 나눗셈 나머지:" + (123 % 100));

System.out.println("실수 나눗셈 : " + (123 / 100.0));

System.out.println(123 / 0.0);

// 정수 0으로 못나눔

// 실수 0.0 으로 나누면 Infinity로 출력

}





* OperatorMain02(복합대입 연산자)


public class OpMain02 {

public static void main(String[] args){

System.out.println("복합 대입 연산자(+=, -=, *=, /=, %=, ...)");

int age = 16;

age = age + 1;

System.out.println("age = " + age);

age += 1;

System.out.println("age = " + age);

int x = 123;

x -= 10;  // x = x - 10;

}// end main()


} // end class OpMain02



* OperatorMain03(증감연산자)


public class OpMain03 {


public static void main(String[] args) {

System.out.println("증감 연산자(++, --)");

int num = 100;

//num = num + 1;

//num += 1;

num++; // 

System.out.println("num = " + num);

int num2 = 100;

++num2;

System.out.println("num2 = " + num2);

int num3 = 100;

int result = ++num3 + 5;

System.out.println("num3 = " + num3);

System.out.println("result = " + result);

int num4 = 100;

result = num4++ + 5;

// result = num4 + 5;

// num4++;

System.out.println("num4 = " + num4);

System.out.println("result = " + result);

// 증감 연산자는 변수 앞(prefix) 또는 변수 뒤(postfix)에

// 사용할 수 있음.

// 증감 연산자가 단독으로 사용되는 경우는 prefix, postfix 모두 동일한 결과

// x++; == ++x;

// 증감 연산자가 다른 연산자와 함께 사용되는 경우는

// prefix는 다른 연산자보다 먼저 계산

// postfix는 다른 연산자보다 나중에 계산

int x = 10;

result = x++ + 5 + ++x;

System.out.println(x);

System.out.println(result);

// 계산 순서 :

// 1. x++ + 5

//   (1) x + 5 ==> 10 + 5 ==> 15

//   (2) x 증가  ==> x = 11

// 2. 15 + ++x

//   (1) ++x   ==> x = 12

//   (2) 15 + 12 ==> 27

// 3. result = 27

} //end main();


} // end class OpMain03 


* OpMain04


public class OpMain04 {


public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("비교 연산자(>, <, >=, <=, ==, !=)");

//비교 연산자의 연산 결과는 true 또는 false

System.out.println(10 > 20);

System.out.println(10 < 20);

System.out.println(123 == 100);

System.out.println(123 != 100);

} // end main()


} // end class OpMain04



*OpMain05


public class OpMain05 {


public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("논리연산자(&&, ||, !)");

// A && B : AND(논리곱) 연산

//    A와 B가 모두 true일때만 true 이고, 나머지는 false

// A || B : OR(논리합) 연산

//    A 또는 B가 true 이면 true 이고, 모두 false일 때는 false

// !A: NOT(논리 부정) 연산

//    A가 true이면 false, A가 false이면 true

System.out.println((10 > 0) && (10 < 100));

System.out.println((10 > 0) || (10 < 100));

System.out.println((10 <= 0) || (10 <= 100));

System.out.println((10 <= 0) && (10 <= 100));

System.out.println(!(111>100));

}




'개발자의 길 > JAVA' 카테고리의 다른 글

JAVA CLASS : 반복문(for, while, loop)  (0) 2016.12.29
JAVA CLASS : 조건문(if)  (0) 2016.12.29
JAVA CLASS : 데이터 타입  (0) 2016.12.29
이클립스(eclipse) 설치  (0) 2016.12.29
이클립스 코딩에 유용한 단축키 모음  (0) 2016.12.29
Comments