package day1227;
class Promotion {
public static void main(String[] args) {
byte num1 = 10;
byte num2 = 20;
int result = 0;
result = num1 + num2;
System.out.println(num1 + "+" + num2 + "=" + result);
}
}
Casting
강제형번환
- 개발자가 필요한 데이터형으로 일시적 변환을 하는 것.
- -원본 값은 유지되지만, 상황에 따라 값 손실이 발생할 수 있음.
-기본형에서 참조형으로 가능, 참조형에서 기본형으로 변환불가능.
-boolean은 동일형으로만 casting 가능.
package day1227;
class Casting {
public static void main(String[] args) {
float d = 2023.12F;
int i = (int)d;
System.out.println(i + "," + d);
float f = (float)1.2345678987;
System.out.println(f);
byte num1 = 10;
byte num2 = 20;
byte result = 0;
result = (byte)(num1 + num2);
System.out.println(result);
char c = '0';
System.out.println(c + "의 유니코드는 " + (int)c);
boolean b = true;
boolean b1 = (boolean)b;
System.out.println(b +","+ b1);
}
}
Constant
constant
package day1227;
class Constant{
public static final int MAX_SCORE=100;
public static void main(String[] args) {
int myScore = 80;
System.out.println("최고점수와 획득점수 간의 차 " + (MAX_SCORE - myScore));
}
}
package day1227;
class UseConstant{
public static final int WIN_SCORE=0;
public static void main(String[] args) {
int myScore = 80;
System.out.println("최고점수: " + Constant.MAX_SCORE +", "+ WIN_SCORE);
System.out.println("Byte의 최고값: " + Byte.MAX_VALUE +
", 최저값:" + Byte.MIN_VALUE);
}
}
데이터형
기본형 데이터형
package day1227;
class PrimitiveDataType {
public static void main(String[] args) {
byte a = 10;
short b = -32768;
int c = -2147483638;
long d = 10;
int cost = 1000000000;
long asset = 2147483648L;
System.out.println("byte : " + a + ", short : " + b + ", int : " + c +
", long :" + d + "모으고 싶은 자산 : " + cost + asset);
char e = 65;
char ff = 48;
char g = 44032;
char h = '\u0000';
System.out.println("char : " + e + ff + g + h);
float f = 12.27F;
float f_ = 12.27f;
double k = 2023.12;
System.out.println("float : " + f +", "+ f_ + " double :" + k);
boolean l = true;
boolean m = false;
System.out.println("boolean : " + l +", "+ m);
}
}
연습문제
package day1227;
class Exam1227 {
public static void main(String[] args) {
char firstInitial = 'H';
char secondInitial = 'G';
char thirdInitial = 'D';
int birth = 2000;
System.out.println("출력 1. 내이름은 홍길동이고, 이니셜은 " + firstInitial + ", " + secondInitial + ", " + thirdInitial + "입니다. 태어난 해는 " + birth + "년으로, 나이는 " + (2023-birth) + "살이다.");
double leftVA = 0.2;
double rightVA = 0.3;
System.out.println("출력 2. 왼눈 시력은 " + leftVA + "이고, 오른눈 시력은" + rightVA + "이고, 양안 시력은 " + (leftVA + rightVA)/2 + "입니다.");
int oneWay = 1500;
System.out.println("출력 3. 편도차비는 " + oneWay + "원이고, 왕복차비는 " + (oneWay*2) + "원이고, 한달 20일 출근하면 월 교통비는 총 " + (oneWay*2)*20 + "원 입니다.");
char aUnicode = 'A';
System.out.println("출력 4. 대문자 'A'의 유니코드 값은 " + (int)aUnicode + "입니다. A+32를 더하면 소문자'" + (char)(aUnicode+32) + "'를 만들 수 있다.");
}
}
package day1227;
class Exam1227_1 {
public static final int MONTH_ALLOWANCE = 300000;
public static final int ALLOWANCE_PER_DAY = 20;
public static void main(String[] args) {
int oneWay = 1500;
int lunchCost = 7500;
System.out.println("한달 용돈은 " + MONTH_ALLOWANCE + "원, 하루 용돈은" + ALLOWANCE_PER_DAY + "일 기준으로 " + (MONTH_ALLOWANCE/ALLOWANCE_PER_DAY) + "원입니다. 하루 지출은 편도 교통비 " +
oneWay + "원, 왕복교통비 " + (oneWay*2) + "원, 점심 식대 " + lunchCost + "원으로, 총 " + ((oneWay*2)*20 + (lunchCost*20)) + "원이 지출됩니다. 한달이 지나면 잔액은 " +
(MONTH_ALLOWANCE - ((oneWay*2)*20 + (lunchCost*20))) + "원입니다.");
System.out.println("byte 최대값 : " + Byte.MAX_VALUE);
System.out.println("byte 최소값 : " + Byte.MIN_VALUE);
System.out.println("double 최대값 : " + Double.MAX_VALUE);
System.out.println("double 최소값 : " + Double.MIN_VALUE);
System.out.println("float 최대값 : " + Float.MAX_VALUE);
System.out.println("float 최소값 : " + Float.MIN_VALUE);
}
}