# 5. 제네릭 (Generics)

## TL;DR

* 형변환을 컴파일러의 도움을 받아서 처리한다.
* 제네릭이 없다면 (Java 5 이전) 컬렉션에서 객체를 꺼낼 때 마다 형변환을 해야 했다.
* 단점 보다는 장점이 많으므로 적극 활용하자!

## 키워드

* 타입 안정성
  * 상수에 대한 타입 안정성은 열거형 (Enum)
  * 복수의 객체를 보관하는 Collection 이나 Class 에서 타입을 컴파일 시점에 처리하는 제네릭 (Generics)
* 와일드카드
  * 공변성
  * 반공변성
  * 무공변성

## 기술적 선택

#### 장점

* 타입 세이프하게 코드를 작성할 수 있다.
* 형변환 관련 오류를 컴파일러 시점에서 발견할 수 있다.

#### 단점

* 코드가 복잡해진다.&#x20;
  * 알고 쓰면 오히려 가독성이 좋아지고 명확하게 사용 가능하다.
  * class 설계 시 타입까지 고려해야 하는 복잡성을 이야기 하는 것 같다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://programmer-jjy.gitbook.io/second-brain/technical/study/effective-java-3-e/5.-generic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
