型変数(Type Variable)
Javaにおけるジェネリック・クラス宣言で用いる型変数(Type Variable)の文法は以下のとおり
TypeParameter:
TypeVariable TypeBoundopt
TypeBound:
extends ClassOrInterfaceType AdditionalBoundListopt
AdditionalBoundList:
AdditionalBound AdditionalBoundList
AdditionalBound
AdditionalBound:
& InterfaceType型変数はオプションで境界を指定できるため,以下のような記述が可能である
//境界持ちの型変数
public static <T extends Object & Comparable<? super T>> T max(){
・・・
}
型引数(Type Arguments)
Javaにおける型引数(Type Arguments)の文法は以下のとおり
TypeArguments:
< ActualTypeArgumentList >
ActualTypeArgumentList:
ActualTypeArgument
ActualTypeArgumentList , ActualTypeArgument
ActualTypeArgument:
ReferenceType
Wildcard
Wildcard:
? WildcardBoundsOpt
WildcardBounds:
extends ReferenceType
super ReferenceTypeこの文法からは以下のような記述が可能である
ArrayList<Integer> hoge//参照型 ArrayList<?> hage; //ワイルドカード ArrayList<? extends Number> hige; //ワイルドカード+上限の境界 ArrayList<? super Integer> huge; //ワイルドカード+下限の境界
型変数にはこれらのワイルドカード,superを用いた下限の境界は指定することはできない
//public <?> void foo(){} こんなのは無い
//public <T super Integer> void bar(){} こんなのも無い