티스토리에 저장했던 글을 옮겼습니다.
https://mrcocoball.tistory.com/53?category=1098996
https://mrcocoball.tistory.com/54?category=1098996
https://mrcocoball.tistory.com/55?category=1098996
https://mrcocoball.tistory.com/56?category=1098996
int age; // int(정수) 타입으로 age 변수 선언
double value; // double(실수) 타입으로 value 변수 선언
age = 30;
int age = 30;
int x = 10;
int y = x;
→ 이 경우 x, y 모두 10
int x = 10;
int y = 20;
int temp = y;
y = x;
x = temp;
※ Python 에서는 x,y = y,x로 변환 (튜플)
public static void main(String[] args) {
int var1; //→ 메소드 블록에서 선언하였으며 블록 내 전체 사용 가능
if(...) {
int var2; //→ if 블록에서 선언하였으며 if 블록에서만 사용 가능
}
for(...) {
int var3; //→ for 블록에서 선언하였으며 for 블록에서만 사용 가능
}
}
long balance = 3333333333333; //→ 컴파일 에러 발생
long balance = 3333333333333L;
char var1 = 'A' // (유니코드 65)
char var2 = 'B' // (유니코드 66)
char var1 = 'A'
int var2 = 'A'
System.out.printIn(var1); //→ A를 출력
System.out.printIn(var2); //→ 65를 출력
char var1 = "A";
char var2 = "리코";
String var1 = "A";
String var2 = "리코";
\t
: 탭만큼 띄움\n
: 줄 바꿈 (라인 피드)\r
: 캐리지 리턴\"
: " 출력\'
: ' 출력\\
: \ 출력\u16진수
: 16진수 유니코드에 해당하는 문자 출력1.4 * 10^-45 ~ 3.4*10^38
, 정밀도 7자리 (소수점 이하 자리)4.9 * 10^-324 ~ 1.8 * 10^308
, 정밀도 15자리 (소수점 이하 자리)float var = 3.14; → 컴파일 에러 발생
float var = 3.14f;
boolean stop = true;
boolean state = false;
- 두 변수의 타입이 동일할 경우, 한 쪽 변수 값을 다른 쪽 변수에 복사해서 저장할 수 있음
- 그러나 변수의 타입이 다를 경우 값의 저장이 가능하거나 불가능할 수 있음
- 변수의 값을 다른 변수로 복사해서 저장할 때 타입의 값이 변환되는데 이를 타입 변환이라고 함
- 예시
byte a = 10; // byte 타입 변수 a에 10을 저장
int b = a; // int 타입 b에 a에 저장되어 있던 10을 복사해서 저장
// → 10은 byte 타입이었으나 b로 옮겨질 때 int 타입으로 변환되었음
- 자동으로 타입 변환이 일어나는 것을 의미
- 값의 허용 범위가 작은 타입이 허용 범위가 큰 타입으로 저장될 때 발생
byte < short < int < long < float < double
byte byteValue = 10;
int intValue = byteValue; // byte -> int로 변환됨
long longValue = 5000000000L;
float floatValue = longValue; // 5.0E9f로 저장됨
double doubleValue = longValue; // 5.0E9로 저장됨
// char 타입의 경우 int 타입으로 자동 타입 변환되면 유니코드 값이 int 타입에 저장됨
char charValue = 'A';
int intValue = charValue // 65가 저장됨
- 예외 : char 타입보다 허용 범위가 작은 byte 타입은 char 타입으로 자동 타입 변환될 수 없음
char 타입은 음수를 포함하고 있지 않으나 byte 타입은 음수를 포함하고 있으므로
int intValue = 10;
byte byteValue = (byte) intValue; // 강제 타입 변환
int intValue = 65;
char charValue = (char) intValue;
System.out.println(charValue); // 'A' 출력
double doubleValue = 3.14;
int intValue = (int) doubleValue; // 소수점 이하 부분 버려지고 3만 저장
int result = byte/char/short/int 연산자(+, -, \*, /, %) byte/char/short/int
에서
byte x = 10;
byte y = 20;
일 경우
int result = x + y;
로 해야 함 (byte result 로 할 시 컴파일 에러) 이 때 x와 y는 int로 변환됨
long result = long 타입 연산자 연산자(+, -, \*, /, %) byte/char/short/int 일 경우
int intValue = 50;
double doubleValue = 5.5;
double result = intValue + doubleValue;
// intValue가 double로 타입 변환 되며 double로 result 값이 저장됨
int intValue = 50;
double doubleValue = 5.5;
int result = intValue + (int) doubleValue; // doubleValue를 int로 강제 변환하여 result 값을 int로 저장
double result = 1.5 + 2.3;
float result = 1.5 + 2.3; // 컴파일 에러
float result = 1.5f + 2.3f;
int x = 1;
int y = 2;
double result = x / y;
System.out.println(result);
// 0.5가 나올 것 같지만 정수끼리의 연산이라
// 0.5에서 소수점 이하 부분 제거되어 0이 되고 double로 출력하여 0.0이 나옴
double x = 1;
int y = 2;
double result = x / y;
System.out.println(result);
String str = "1" + (2+3); -> "1" + 5 => "15"
변환 타입 | 사용 예 |
---|---|
String -> byte | String str = "10"; byte value = Byte.parseByte(str); |
String -> short | String str = "200"; short value = Short.parseShort(str); |
String -> int | String str = "30000"; int value = Int.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 = Boolean.parseBoolean(str); |