Core Java

Java Generics Quick Tutorial

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

38 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Venkataramana Madugundu

The section ‘Subtyping of Generic Types’ is just what I wanted to understand from the perspective of compiler. And your explanation is absolutely clear. I was just lazy for a long time to understand this stuff. And you just opened my eyes. Thanks a ton. Venkata Madugundu

Manvendra Singh
13 years ago

nice one

Binh Thanh Nguyen
12 years ago

It’s so useful to me. Thanks

Rajendra
Rajendra
12 years ago

Very neat explanation. Thanks.

Anil Kumar
Anil Kumar
12 years ago

Nice explanation,

JR
JR
12 years ago

A very good explanation; concise, and easy to understand.

However, in the snippet,

public static T getFirst(List list)

shouldn’t in fact be ? This corresponds to the example in the official tutorial,

static void fromArrayToCollection(T[] a, Collection c) {
for (T o : a) {
c.add(o); // Correct
}
}

but, in that case, everything is consistent. If is correct, I’m missing something basic — please explain.

Thanks.

JR
JR
12 years ago
Reply to  JR

Angle brackets not showing after submission, I’ll try again, where – represents brackets:

public static -t- T getFirst(List-T- list)

and

static -T- void fromArrayToCollection(T[] a, Collection-T- c) {
for (T o : a) {
c.add(o); // Correct
}
}

Loren
3 years ago
Reply to  JR

It took me a while to understand what was written here, indeed you made a good remark on a very good topic

yuva
yuva
12 years ago

Good one

Cris Berneburg
Cris Berneburg
11 years ago

Enrico, thank you so much for this Generics tutorial. It has helped me a great deal in understanding Generics. However, I am still struggling to understand the wildcard. In your example before the PECS section, the “List = fruits;” line is missing the left-variable name, which confuses me. Should it be “List apples = fruits;” instead? In the next lines then, the “fruits” reference should be replaced with “apples”. It would follow that these would compile fine: apples.add(new Apple()); apples.add(new GreenApple()); But these would generate a compiler error: apples.add(new Fruit()); apples.add(new Object()); Please let me know if I am on… Read more »

Cris Berneburg
Cris Berneburg
11 years ago

Enrico, thank you so much for this Generics tutorial. It has helped me a great deal in understanding Generics. However, I am still struggling to understand the <? super T> wildcard. In your example before the PECS section, the “List<? super Apple> = fruits;” line is missing the left-variable name, which confuses me. Should it be “List<? super Apple> apples = fruits;” instead? In the next lines then, the “fruits” reference should be replaced with “apples”. It would follow that these would compile fine: apples.add(new Apple()); apples.add(new GreenApple()); But these would generate a compiler error: apples.add(new Fruit()); apples.add(new Object()); Please… Read more »

Enrico Maria Crisostomo

Hi Cris, Yes, that’s a typo. Let’s suppose the unnamed list is called “apples”. What can you add to that list? It is a list of objects of an unknown type which is a super-type of Apple, hence you will be able to add only Apple instances (and subtypes, of course, because they *are* instances of Apple). The third and fourth lines of the example just stress this: you cannot add a Fruit (although the original list was a list of Fruit), since a Fruit may not be an Apple, so it may not be an Apple supertype. The PECS… Read more »

jones
6 years ago

it was really true and agree with you

Prem
9 years ago

good explanation with good examples. Thanks a lot Team..

Eobard
Eobard
9 years ago

public interface Juicy {
Juice squeeze();
}

What does this even mean? An interface called Juicy that has a method squeeze() that returns the interface Juciy ???

Priya
7 years ago

By using generics you can define an algorithm once, and you can apply it on any kind of datatype without any additional effort.

jones
6 years ago

thanks for sharing useful informative guide

mike
6 years ago

it was a great tutorial indeed about the java generics

ben
5 years ago
Reply to  mike

yes, it was nice and cool to see the tutorials here at site

Myra Dixit
5 years ago

Hii Enrico Crisostomo,
Thank you so much for posting such an informative and valuable blog, which can help each newcomer in the field of Java and any other programming language. To know more, visit http://www.agileacademy.co.in/java-development/

Snehal Yadav
5 years ago

Great post. This post is very informative and useful for me. Thank you for sharing this useful tutorial.

blog vile
5 years ago

Thank you so much for sharing this post, I appreciate your work.

My Gyan Guide
5 years ago

Good explain. Thanks for sharing.

Technology

Stephanie
5 years ago

Some really useful information including great examples as well for clear understanding. Love it, thanks

Eddie
4 years ago
Reply to  Stephanie

I bet you already knew this info, Stef! Great article, keep it like that!

Back to top button