short a, b;

a = 10;
b = 5;
/*

b = b + a; 는 컴파일 오류임. +연산은 int로 형 변환이 일어나기 때문이다.

따라서 b = (short)(b + a); 로 해야한다.

*/
b += a; 
System.out.println(b);

15

/*

b +=a; 는 형 변환이 일어나지 않는 연산이다. b=b+a와 결과가 동일하다.

*/
char c = 'A';
c += 10;
System.out.println(c); 

/*

65+10 = 75 에 해당하는 char는 K 이다.

*/
float f = 3.5f;
char ch = 'A';
ch += f;
System.out.println(ch);

/*

65+3.5 = 68.5 이지만 char에 들어갈때 알아서 정수가 들어간다. 68에 해당하는 char는 D.

*/

'쌍용강북교육센터 > 7월' 카테고리의 다른 글

0705_Ex16_Operator  (0) 2021.07.06
0705_Ex15_Operator  (0) 2021.07.06
0705_Ex13_Bit  (0) 2021.07.06
0705_Ex12_Bit  (0) 2021.07.06
0705_Ex11_Bit  (0) 2021.07.06

+ Recent posts