티스토리 뷰
반응형
[PromotionExample.java] 자동 타입 변환
package july22_chap2;
public class clon_sec03_ex1 {
public static void main(String[] args) {
byte byteValue = 10;
int intValue = byteValue; //byte는 int보다 작기 때문에 자동변환
System.out.println(intValue);
char charValue = '가';
intValue = charValue; //char는 int타입으로 변환되면 유니코드값이 int값에 저장
System.out.println("가의 유니코드= " + intValue);
intValue = 500;
long longValue = intValue; //int는 long보다 작기 때문에 자동변환
System.out.println(longValue);
intValue = 200;
double doubleValue = intValue; //double은 실수 표시기 때문에 소수점이 나타남
System.out.println(doubleValue);
}
}
</>
10
가의 유니코드= 44032
500
200.0
[CastingExample.java] 강제 타입 변환
package july22_chap2;
public class clon_sec03_ex2 {
public static void main(String[] args) {
int intValue = 44032;
char charValue = (char) intValue; //44032가 char에선 '가'임
System.out.println(charValue);
long longValue = 500;
intValue = (int) longValue; //8byte 중 끝의 4byte로 300표현 가능
System.out.println(intValue);
double doubleValue = 3.14;
intValue = (int) doubleValue; //소수점 이하 부분은 버려지고 정수만 저장됨
System.out.println(intValue);
}
}
</>
가
500
3
[CheckValueBeforeCasting.java] 변환으로 인한 데이터 손실이 발생되지 않도록 한다.
package july22_chap2;
public class clon_sec03_ex3 {
public static void main(String[] args) {
int i = 128; //byte는 -128부터 127까지
if((i<Byte.MIN_VALUE) || (i>Byte.MAX_VALUE)) {
System.out.println("byte 타입으로 변환할 수 없습니다.");
System.out.println("값을 다시 확인해 주세요.");
} else {
byte b = (byte) i;
System.out.println(b);
}
}
}
</>
byte 타입으로 변환할 수 없습니다.
값을 다시 확인해 주세요.
[FromIntToFloat.java] 정수 타입을 실수 타입으로 변환할 때 정밀도 손실을 피한다.
package july22_chap2;
public class clon_sec03_ex4 {
public static void main(String[] args) {
int num1 = 123456780;
int num2 = 123456780;
double num3 = num2;
System.out.println(num3); //num3 = 1.2345678E8
num2 = (int) num3;
System.out.println(num2); //num2 = 123456780
int result = num1 - num2;
System.out.println(result);
}
}
</>
1.2345678E8
123456780
0
[FromIntToDouble.java] 정수 타입을 실수 타입으로 변환할 때 정밀도 손실을 피한다.
package july22_chap2;
public class clon_sec03_ex5 {
public static void main(String[] args) {
int num1 = 123456780;
int num2 = 123456780;
float num3 = num2;
System.out.println(num3); //1.23456784E8
num2 = (int) num3;
System.out.println(num2); //123456784
int result = num1 - num2;
System.out.println(result);
//123456780 - 123456784 = -4
//실수 값 계산하려면 double을 쓰자.
}
}
</>
1.23456784E8
123456784
-4
[OperationsPromotionExample.java] 연산식에서 자동 타입 변환
public static void main(String[] args) {
byte byteValue1 = 10;
byte byteValue2 = 20;
//byte byteValue3 = byteValue1 + byteValue2 byte끼리 연산하려면 int로
int intValue1 = byteValue1 + byteValue2;
System.out.println(intValue1);
char charValue1 = 'A';
char charValue2 = 1;
//char charValue3 = charValue1 + charValue2;
int intValue2 = charValue1 + charValue2; //65 + 1
System.out.println("유니코드= " + intValue2); //66
System.out.println("출력문자= " + (char)intValue2); //B
int intValue3 = 10;
int intValue4 = intValue3/4; //10 나누기 4하고 정수만 출력
System.out.println(intValue4);
int intValue5 = 10;
//int intValue6 = 10 / 4.0 ; // 정수와 실수를 연산할 수 없음
double doubleValue = intValue5 / 4.0; //소수점까지 표현가능
System.out.println(doubleValue);
}
}
</>
30
유니코드= 66
출력문자= B
2
2.5
반응형
'Language > JAVA' 카테고리의 다른 글
아홉째날 [상속 (2) 추상 클래스] (0) | 2022.08.03 |
---|---|
Chapter3. 연산자 .3 단항 연산자 (0) | 2022.08.02 |
Chapter2. 변수와타입 .2 데이터타입 (0) | 2022.08.02 |
Chapter2. 변수와 타입 .1변수 (0) | 2022.08.02 |
Chapter1. 자바 시작하기 (0) | 2022.08.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Date 클래스
- FALSE
- java.time.package
- Pattern 클래스
- Arrays 클래스
- 포장 클래스
- 문자열함수
- 기본 API 클래스
- StringBuilder 클래스
- Format 클래스
- 자료형
- 요소선택
- 리스트연산자
- Math 클래스
- not_in
- StringTokenizer 클래스
- 딕셔너리
- 스레드 스케줄링
- 순환할당
- IndexError
- 프로그램
- Objects 클래스
- 역반복문
- 함수
- python
- StringBuffer 클래스
- Random 클래스
- 리스트
- 파이썬
- Calendar 클래스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
글 보관함