티끌모아 태산

[JAVA] Generic에서 E와 T 차이 본문

카테고리 없음

[JAVA] Generic에서 E와 T 차이

yesman9 2024. 2. 2. 13:15

https://stackoverflow.com/questions/6008241/what-is-the-difference-between-e-t-and-for-java-generic

 

What is the difference between 'E', 'T', and '?' for Java generics?

I come across Java code like this: public interface Foo<E> {} public interface Bar<T> {} public interface Zar<?> {} What is the difference among all three of the above and wha...

stackoverflow.com

위 링크에서 답변 중 아래와 같은 내용이 있다.

It's more convention than anything else.

  • T is meant to be a Type
  • E is meant to be an Element (List<E>: a list of Elements)
  • K is Key (in a Map<K,V>)
  • V is Value (as a return value or mapped value)

They are fully interchangeable (conflicts in the same declaration notwithstanding).

The letter between the < > is just a name. What you describe in your answer are just conventions. It doesn't even have to be a single, upper-case letter; you can use any name that you like, just like you can give classes, variables etc. any name you like. 

요약하자면 아래와 같다.

  • T는 Type
  • E는 Element (List안의 원소 등)
  • K는 Map에서의 Key
  • V는 Map에서의 Value

하지만 이것들은 단지 이름에 불과하며, 서로 바꾸어 써도 아무 문제가 없다. 원하는 이름을 써도 상관없다.

결론: 그냥 이름이다