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
It took me a while to understand what was written here, indeed you made a good remark on a very good topic
yuva
12 years ago
Good one
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
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 »
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 »
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/
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
nice one
It’s so useful to me. Thanks
Very neat explanation. Thanks.
Nice explanation,
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.
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
}
}
It took me a while to understand what was written here, indeed you made a good remark on a very good topic
Good one
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 »
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 »
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 »
it was really true and agree with you
good explanation with good examples. Thanks a lot Team..
public interface Juicy {
Juice squeeze();
}
What does this even mean? An interface called Juicy that has a method squeeze() that returns the interface Juciy ???
By using generics you can define an algorithm once, and you can apply it on any kind of datatype without any additional effort.
thanks for sharing useful informative guide
it was a great tutorial indeed about the java generics
yes, it was nice and cool to see the tutorials here at site
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/
Great post. This post is very informative and useful for me. Thank you for sharing this useful tutorial.
Thank you so much for sharing this post, I appreciate your work.
Good explain. Thanks for sharing.
Technology
Some really useful information including great examples as well for clear understanding. Love it, thanks
I bet you already knew this info, Stef! Great article, keep it like that!