Page 45 - 든든한 Java programming 도서 미리보기
P. 45

CHAPTER 2? 변수와 연산자  39

표 2-4? 산술연산자      사용법                의미
                  x+y    x와 y를 더한 결과 값
            연산자   x-y      x와 y를 뺀 결과 값
               +  x*y    x와 y를 곱한 결과 값
               -  x/y    x와 y를 나눈 몫의 값
               *  x%y  x와 y를 나눈 나머지의 값
               /
               %

산술연산자는 자바의 원시 데이터 타입에서 boolean 타입을 제외한 모든 타입에서 사
용이 가능하다. 단 % 연산자의 경우 피연산자로 정수 타입의 데이터만 취한다. [코드
2-10]은 산술연산자의 예를 보여주는 프로그램이다.

     코드 2-10 산술 연산자

 1? public class Example {
 2? public static void main(String[] args) {
 3? int result;
 4?
 5? result = 7+5;
 6? System.out.println("7 + 5 = " + result);
 7?
 8? result = 7-5;
 9? System.out.println("7 - 5 = " + result);
10?
11? result = 7*5;
12? System.out.println("7 * 5 = " + result);
13?
14? result = 7/5;
15? System.out.println("7 / 5 = " + result);
   40   41   42   43   44   45   46   47   48   49   50