quilt code
[Java] 타입 변환 본문
int v2 = 1e2;
float v3 = 3.14;
double 타입이라 int 나 float에 넣는게 원칙적으로는 안됨 -> 이걸 가능하게 하려면 타입 변환을 해야함
1. 자동타입변환
- 작은허용범위->큰허용범위
- byte<short<int<long<float<double
byte byteValue = 10;
int intValue =byteValue;
-> 자동타입변환됨
2. 강제타입변환
- 큰허용범위->작은허용범위
- 작은허용범위 = (작은허용범위) 큰허용범위
3. 정수 연산에서의 자동 타입 변환
byte + byte => int (int보다 작은 것을 연산하게 되면)
ex) int result = x + y;
연산 결과를 int 변수에 저장
4. 실수 연산에서의 자동 타입 변환
| 방법1 | int x = 1; int y = 2; double result = (double) x / y; System.out.println(result); |
| 방법2 | int x = 1; int y = 2; double result = x / (double) y; System.out.println(result); |
| 방법3 | int x = 1; int y = 2; double result = (double) x / (double) y System.out.println(result); |
ex) int x = 1;
int y = 2;
double result =x/y; -> int로 저장됨 (0.5에서 0만 남음) -> 실수의 결과를 알고 싶으면 (double)
5. 연산에서의 문자열 자동 타입 변환
7+10=17
"7"+10 =불가능 : 문자+숫자 -> 문자 결합의 형태 : 710의 형태가 됨 (문자가 있으면 결과도 문자)
"7"x10=에러
숫자를 문자열로 바꾸기
피연산자 중 하나가 문자열일 경우에는 나머지 피연산자도 문자열로 자동 변환되어 문자열 결합 연산 수행
int value = 3 + 7; -> int value = 10;
String str = "3" + 7; -> String str = "3" + "7" -> String str = "37";
" " 이후 뒤에 다 문자열로 인식
6. 문자열을 기본 타입으로 강제 타입 변환
| String -> byte | String str = "10"; byte value = Byte.parseByte(str); |
| String -> short | String str = "200"; short value = Short.parseShort(str); |
| String -> int | String str = "300000"; int value = Integer.parseInt(str); |
| String -> long | String str = "40000000000"; long value = Long.parseLong(str); |
| String -> float | String str = "12.345"; float value = Float.parseFloat(str); |
| String -> double | String str = "12.345"; double value = Double.parseDouble(str); |
| String -> boolean | String str = "true"; boolean value = Boolea.parseBooolean(str); |
1. 자동 타입 변환


2. 강제 타입 변환


3. 정수 타입의 연산


4. 정수 타입의 연산


5. 연산식에서 자동 타입 변환

- byte byteValue3 = byteValue1 + byteValue2; 컴파일 에러
- char charValue3 - charValue1 + charValue2; 컴파일 에러
- int intValue6 = 10 / 4.9; 컴파일 에러

p. 85
1. **byte byteValue = 10;
char charValue = 'A'; **
short shortValue = charValue; :short은 음수 존재 char는 음수x
2.
| int intValue = 10; |
| char charValue = 'A'; |
| double doubleValue = 5.7; |
| String strValue = "A"; |
1) double var = (double)intValue;
2) byte var = (byte)intValue;
3) int var = (int)doubleValue; 문자열=/=문자열
4) char var = (char)strValue;
3.
| byte byteValue = 10; |
| float floatValue = 2.5F; |
| double doubleValue = 2.5F; |
1) byte result = byteValue + byteValue;
2) int result = 5 byteValue;
3) float result = 5 + floatValue;
4) double result = 5 + doubleValue;
byte + byte이면 더 큰 int로
4.
| short s1 = 1; | |
| short s2 = 2; | |
| int i1 = 3; | |
| int i2 = 4; | |
| short result = s1 + s2; | int result = s1 + s2; |
| int result = i1 + i2; |
5.
| char c1 = 'a'; | |
| char c2 = c1 + 1; | char c2 = (char) (c1 + 1); |
| System.out.println(c2); |
6.
| int x = 5; |
| int y = 2; |
| int result = x / y; |
| System.out.pritnln(result); |
=> 2
7.
| int x = 5; |
| int y = 2; |
| double result = (double) x / y |
| System.out.println(result); |
8.
| double var1 = 3.5; |
| double var2 =2.7; |
| int result = (int) (var1 + var2); |
9.
| long var1 = 2L; |
| float var2 = 1.8f; |
| double var3 = 2.5; |
| String var4 = "3.9"; |
| int result = (int) var1 + (int) (var2 + var3) + (int) Double.parseDouble(var4) |
| System.out.println(result); |
문자열을 기본타입으로 바꾸고Double.parse 정수로 바꿔서(int) 더해줌
10.
| String str1 = 2+ 3 + " " | 5 |
| String str2 = 2 + " " + 3 | 23 |
| String str3 = " "+ 2 + 3 | 23 |
| System.out.println(result); | |
| System.out.println(result); | |
| System.out.println(result); |
11.
| byte value = Byte.parseByte |
| int value = Integer.parseInt |
| float value = Float.parseFloat |
| double value = Double.parseDouble |
'daily > Java' 카테고리의 다른 글
| [Java] 연산자 (0) | 2022.12.23 |
|---|---|
| [Java] 변수와 시스템 입출력 (0) | 2022.12.22 |
| [Java] 타입 (0) | 2022.12.21 |
| [Java] 확인 문제 (0) | 2022.12.20 |
| [Java] 변수 (0) | 2022.12.20 |