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

Variable type and literal type mismatch

  1. range of Variable > range of Literal : OK

2. range of Variable < range of Literal : Error

3. byte, short Variable contains int literal

: 단, 변수의 타입의 범위 이내여야 함.

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 크기를 가진다.

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?