Jump to content

Talk:Java Programming/Generics

Page contents not supported in other languages.
Add topic
From Wikibooks, open books for an open world
Latest comment: 16 years ago by Subanark in topic Things that are needed

I am wondoring where the name 'Generics' comes from. Does anyone knows? Ervinn21:07, 27 July 2006 (UTC)Reply

Use Generics other than Collection classes

[edit source]

Is there a need to use Generics other than using it for Java Collections? I am looking for an example ,other than Java Collection classes, where the use of Generics would be handy. Currently I do not see one. Ervinn15:49, 18 August 2006 (UTC)Reply

Things that are needed

[edit source]
  • Generic type parameter in throws clause

public <T extends Throwable> throwMe(T exception) throws T {throw exception;}

  • Subclasses of Throwable cannot be generic
  • Cannot create arrays of generic types or pass generic types as arguments to varargs.
  • Generic constructors
  • special compiler handling of the getClass() method:

Rectangle rect = ...; Class<? extends Rectangle> cls = rect.getClass();

  • Deep generics (a list that contains either lists of Strings or Objects):

List<List<? super String>> list;

  • Generics and autoboxing:

List<Boolean> list = new List<Boolean>(); list.add(true); list.add(false);

  • Generics and inner classes

Subanark (talk) 23:54, 2 August 2008 (UTC)Reply

Needed

[edit source]

Sections for the following structures are needed:


<T extends Class1 & Interface1 & Interface2> void method(T parameter);

<T1, T2> void method(T1 t1, T2 t2);

close