데이터 타입 | 메모리 크기 | 최솟값 | 최댓값 | 기본값 |
---|---|---|---|---|
byte | 1byte | -128 | 127 | 0 |
short | 2byte | -32,768 | 32,767 | 0 |
int | 4byte | -2**31 또는 0 | 2**31-1 또는 (2**32)-1 | 0 |
long | 8byte | -2**62 또는 0 | 2**62-1또는 (2**63)-1 | 0L |
float | 8byte | -3.4E38 | 3.4E38 | 0.0F |
double | 8byte | -1.7E308 | 1.7E308 | 0.0 |
boolean | 1byte | false | true | false |
char | 2byte(유니코드) | '\u000'(0) | '\ufff'(65,535) | '\u000' |
데이터 타입
과 힙 메모리에 저장된 객체 접근하기 위한 일종의 포인터
는 스택 메모리
에 저장되며, 객체
는 힙 메모리
에 저장한다. 객체를 사용할 때마다 참조 변수에 저장된 객체의 주소를 불러와 사용하는 방식이다.스택 메모리 | 힙 메모리 |
---|---|
Value Types | Reference Types |
Pointers to the Reference Type |
제네릭 타입(Generic Type)이란?
- 데이터 형식에 의존하지 않고 하나의 값이 여러 다른 데이터 타입을 가질 수 있도록 하는 방법이다.
- 클래스 내부에서 사용할 데이터 타입을 외부에서 지정하는 기법
제네릭 타입의 특징
제네릭 타입에는 참조 타입만 할당 받을 수 있다. 이 때 int
, double
과 같은 원시 타입을 제네릭 타입 매개변수로 사용할 수 없으므로 이를 대체할 수 있는 Integer
, Double
과 같은 Wrapper 클래스를 사용한다.
class Person<T> {
public T info;
}
public class GenericDemo {
public static void main(String[] args) {
Person<String> p1 = new Person<String>();
Person<StringBuilder> p2 = new Person<StringBuilder>();
}
}
class EmployeeInfo {
public int rank;
Employee(int rank) {this.rank = rank};
}
class Person<T, S> {
public T info;
public S id;
Person(T info, S id) {
this.info = info;
this.id = id;
}
}
class GenericDemo {
public static void main(String[] args) {
EmployeeInfo emp = new EmployeeInfo(1);
Integer id = new Integer(10);
Person p1 = new Person(emp, id);
// Person<EmployeeInfo, Integer> p1 = new Person<EmployeeInfo, Integer>(emp, id);
}
}