public class ch3_OperatorEx15 {
public static void main(String[] args) {
char lowerCase = 'a';
char upperCase = (char)(lowerCase - 32);
System.out.println(upperCase);
}
}
대문자와 소문자의 코드값의 차이는 32이다.
char형과 int형의 연산결과는 int형이므로다시 형변환을 해줘야한다.
public class ch3_OperatorEx16 {
public static void main(String[] args) {
float pi = 3.141592f;
float shortPi = (int) (pi*1000) / 1000f;
System.out.println(shortPi);
}
}
(int)(pi*1000) / 1000f;
(int)(3141.592f) / 1000f;
3141 / 1000f; // 반올림이 아닌 버림이 일어난다.
3.141f
public class ch3_OperatorEx17 {
public static void main(String[] args) {
double pi = 3.141592;
double shortPi = Math.round(pi * 1000) / 1000.0;
System.out.println(shortPi);
}
}
Math.round()
를 이용public class ch3_OperatorEx19 {
public static void main(String[] args) {
int x = 10;
int y = 8;
System.out.printf("%d을 %d로 나누면, %n", x, y);
System.out.printf("몫은 %d이고, 나머지는 %d입니다. %n", x/y, x%y);
}
}
"%" 연산자는 나머지를 반환하는 연산자이다.
public class ch3_OperatorEx20 {
public static void main(String[] args) {
System.out.println(-10%8); // 왼쪽 피연산자가 음수인 경우 음수가 출력된다.
System.out.println(10%8);
System.out.println(-10%-8); // 오른쪽 피연산자의 부호는 결과값에 영향을 미치지 않는다.
}
}