Variable Type
Variable is depend on what value it will take.
8 types of value
-문자 : char
-숫자 : 정수 - byte, short, int, long/실수- float, double
-논리 : boolean
byte: It is used for binary data,
short: It is used for Compatibility with C.(Not used well)
Variable(변수), Constant(상수), Literal(리터럴)
Variable(변수) : 하나의 값을 저장하기 위한 공간. (변경O)
Constant(상수) : 한 번만 값이 저장 가능한 변수. 변수와 똑같이 선언하되, 앞에 "final"을 붙인다. (변경X)
Literal(리터럴) : 값 그 자체를 의미한다.
int score = 100;
score = 200;
final int MAX = 100;
MAX = 200;//에러
Prefix and suffix of Literal
Type
Literal
Suffix(접미사)
논리형
false, true
X
정수형
123, 0b0101, 077, 0xFF, 100L
L
실수형
3.14, 3.0e8, 1.4f, 0x1.0p-1
f,d(생략가능)
문자형
'A', '1', '\n' (single quotation)
X
문자열
"ABC", "123", "A", "true" (double quotation)
X
//byte가 저장할 수 있는 값의 변수는 -128 ~ 127
byte b = 127;//정수값인데 접미사가 없다. byte는 int형 리터럴을 사용한다.
byte b = 128;//에러
int i = 100;//10진수
int oct = 0100;//8진수
int hex = 0x100;//16진수
int bin = 0b0101;//2진수
//long변수 사용 - 20억을 초과하는 값에는 suffic "L" 잊지 말기!
long l = 10_000_000_000L;//100억. int형 변수가 담을 수 있는 최댓값은 약 20억.
long l = 100;//20억을 넘지 않기 때문에 suffic 생략가능.
float f = 3.14f;//'f' is essential
double d = 3.14d;//suffix 'd' is omitable(생략가능)
//소수점이나 f나 e가 포함되어있다면 실수형이라는 것을 알자!
10. = 10.0
.10 = 0.10
//=>둘다 실수형.접미사가 안 붙었으니 double 타입이다.
10f = 10.0f
//=>float형
1e3 = 1000.0
//e는 10의 n제곱을 의미.
Variable type and literal type mismatch
range of Variable > range of Literal : OK
int i = 'A';//int > char. the value of 'A' is 65.
long l = 123;//long > int
double d = 3.14f;// double > float
2. range of Variable < range of Literal : Error
int i = 30_0000_0000;//int의 범위(+_20억) 벗어남
long l = 3.14f;//long(8byte) < float(4byte). 실수형float이 정수형long보다 저장 범위가 넓기 때문에 에러 발생!
float f = 3.14;// float < double.d가 생략되면 double인 것을 잊으면 안 된다!
3. byte, short Variable contains int literal
: 단, 변수의 타입의 범위 이내여야 함.
byte b = 100;
byte b = 128;//에러. byte의 범위(-128~127)을 벗어남.
Primitive type(기본형)과 Reference type(참조형)
Primitive type(기본형)
: 오직 8개(boolean, char, byte(1byte), short(2byte), int(4byte), long(8byte), float, double)
실제 값을 저장한다.
Reference type(참조형)
: 기본형을 제외한 나머지(String, System 등)
객체의 주소를 저장한다. 메모리 주소를 저장하기 때문에 항상 4byte 또는 8byte 크기를 가진다.
Date today;//참조형 변수 today 선언
today = new Date();//today에 객체의 주소를 저장한다.
4byte로 표현할 수 있는 최댓값은 대략 4,000,000,000(40억)이기 때문에, 4GB의 메모리를 다룰 수 있다. JVM에서 사용하는 메모리를 제외하면 실제로 프로그램이 사용가능한 메모리는 2GB 정도에 불가하다.
32bit JVM에서 참조변수의 크기는 4byte이고, 64bit JVM에서 참조변수의 크기는 8byte이고, 다룰 수 있는 최대 메모리는 40억X40억 = 16경byte = 1600만TB다.(이론), 실제로는 1TB 정도이다.
Last updated
Was this helpful?