- 컴파일 과정에서 타입 체크를 해주는 기능이다.
- 사용할 수 있는 객체 타입을 컴파일 시에 미리 체크를 해주기 때문에 타입 안정성을 높이고, 형변환의 번거로움을 덜어준다.
-
ex) Collection에 특정 객체만 추가할 수 있도록 하는 것이 제네릭이다. 제네릭을 사용하면 Collection 내부에 들어온 객체가 내가 의도한 값인지 별도로 체크하는 로직을 구현할 필요가 없어진다.
- 객체 생성방법 : 생성자 패턴, 정적 메소드 패턴, 수정자 패턴(Setter), 빌더 패턴
- 빌더 패턴을 사용하여 객체 생성시 장점
- 필요한 데이터만 설정가능
: 동적으로 필요한 데이터만 객체 생성시 초기화할 수 있다. - 유연성
: 클래스에 새로운 변수 추가해야 하는 경우, 기존의 코드에 영향을 주지 않고 변수를 추가할 수 있다. - 가독성
: 변수에 설정되는 값을 쉽게 파악할 수 있다.
- 필요한 데이터만 설정가능