Generics

Generics

Effective Java 2nd Edition

Agile Java

Genercis tutorial

Effective Java Reloaded

Summary on some java generics presentations/postings

자바 이론과 실습: 제네릭스 해부, Part 1

Generic 팩토리 메소드

 

자바 이론과 실습: 제네릭스 해부, Part 2

http://www.ibm.com/developerworks/java/library/j-jtp07018.html

 

Generic 팩토리 메소드

Super Type Tokens

Generics gotchas

Generic 메타데이터 활용하기

 

InstanceCreator

[Generic] 자바 Generic 타입 알아내기

Generic 메타데이터 활용하기

 

 

 

 

 

 

일반적으로 자바 컴파일러는 입력변수로 주어진 제네릭 자료형에 대해서는 와일드카드의 하한경계를 지정함으로써(super 키워드 사용) 자료형의 제약을 풀수 있고, 반환유형으로 주어진 제네릭 자료형에 대해서는 와일드카드의 상한경계(extends 키워드 사용)를 지정함으로써 자료형의 제약을 풀 수 있다.

 

새 인스턴스 생성

 public static <T> T createObject(Class<T> clazz) throws Exception{
     return clazz.newInstance();
 }


 

Java_제네릭스에_대한_실제적_고찰.pdf

 

Effective Java 2nd Edition

 

Item 23: Don't use raw types in new code

Raw type을 사용한다면 genercis를 사용할 때의 장점인 안전성과 표현력을 다 잃어버린다.

List<String>은 List<Object>의 하위 클래스가 아니다.

 

Item 24: Eliminate unchecked Warnings

할 수 있는 모든 warning을 제거하고, 그렇게 할 수 없는 것은 @SupressWarnings("unchecked") annotation을 가능한한 가장 좁은 범위로 선언하고, 그렇게 해도 안전한 이유를 주석으로 달아라.

 

Item 25 : Prefer lists to arrays

 

public static <T extends Comparable<? super T>> T max(List<T> list){
  Iterator<? extends T> i = list.iterator();
  T result = i.next();
  while(i.hasNext()){
            T t = i.next();
            if (t.compareTo(result)>0) result = t;
   }
   return result;
}