• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Question about generics/wildcards - my code wont'compile!

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I'm currently doing the review questions on chapter 3: Generics and Collections in the study guide by Jeanne and Scott. In my IDE I'm experimenting with some code, inspired by the review questions from chapter 3 (mainly question 5 and 7). I can't figure out for the life of me why this code won't compile!



This generates:

Error:(6, 16) java: no suitable method found for add(java.lang.Integer)
   method java.util.Collection.add(capture#1 of ? extends java.lang.Number) is not applicable
     (argument mismatch; java.lang.Integer cannot be converted to capture#1 of ? extends java.lang.Number)
   method java.util.Set.add(capture#1 of ? extends java.lang.Number) is not applicable
     (argument mismatch; java.lang.Integer cannot be converted to capture#1 of ? extends java.lang.Number)

Can anyone give me some insight on what this means and if I should know this for the exam? Any help is appreciated!
 
Enthuware Software Support
Posts: 4575
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henk Smit wrote:Hello all,

I'm currently doing the review questions on chapter 3: Generics and Collections in the study guide by Jeanne and Scott. In my IDE I'm experimenting with some code, inspired by the review questions from chapter 3 (mainly question 5 and 7). I can't figure out for the life of me why this code won't compile!



This generates:

Error:(6, 16) java: no suitable method found for add(java.lang.Integer)
   method java.util.Collection.add(capture#1 of ? extends java.lang.Number) is not applicable
     (argument mismatch; java.lang.Integer cannot be converted to capture#1 of ? extends java.lang.Number)
   method java.util.Set.add(capture#1 of ? extends java.lang.Number) is not applicable
     (argument mismatch; java.lang.Integer cannot be converted to capture#1 of ? extends java.lang.Number)

Can anyone give me some insight on what this means and if I should know this for the exam? Any help is appreciated!



"? extends Number" does not mean "any class that extends number". It means a specific but unknown class that extends Number. Thus, if you have a collection of "? extends Number" objects, you can't add an Integer to it because it is not necessarily a collection of Integers. But if you take something out of that collection you are sure that it is at least a Number.
 
Henk Smit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:

Henk Smit wrote:Hello all,

I'm currently doing the review questions on chapter 3: Generics and Collections in the study guide by Jeanne and Scott. In my IDE I'm experimenting with some code, inspired by the review questions from chapter 3 (mainly question 5 and 7). I can't figure out for the life of me why this code won't compile!



This generates:

Error:(6, 16) java: no suitable method found for add(java.lang.Integer)
   method java.util.Collection.add(capture#1 of ? extends java.lang.Number) is not applicable
     (argument mismatch; java.lang.Integer cannot be converted to capture#1 of ? extends java.lang.Number)
   method java.util.Set.add(capture#1 of ? extends java.lang.Number) is not applicable
     (argument mismatch; java.lang.Integer cannot be converted to capture#1 of ? extends java.lang.Number)

Can anyone give me some insight on what this means and if I should know this for the exam? Any help is appreciated!



"? extends Number" does not mean "any class that extends number". It means a specific but unknown class that extends Number. Thus, if you have a collection of "? extends Number" objects, you can't add an Integer to it because it is not necessarily a collection of Integers. But if you take something out of that collection you are sure that it is at least a Number.



Thank you!

Your explanation made me realize that this IS covered in the book; using upper-bounded wildcards or an unbounded wildcard makes a collection logically immutable.

I feel silly now.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is the moment you put a collection with a reference bound(upper-bound), it is now immutable.
Before you assign the collection object to a bounded reference, you should modify it head on.

List<Bird> birds1 = new ArrayList<>();
birds1.add(new Sparrow());
birds1.add(new Bird());
List<? extends Bird> birds2 = birds1;
//birds2.add(new Sparrow());
//birds2.add(new Bird());

If you remove the comment, the code will not compile.
 
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kertes Gray wrote:The answer is the moment you put a collection with a reference bound(upper-bound), it is now immutable.


Hi Kertes, this is not entirely true though. The collection is mutable, you can sort it, clear it, delete elements at certain indexes from it. You just cannot call add method on it as the parameter <? extends Bird> won't take any type of object as a valid argument. The only way to call add on a List<? extends Bird> is to do something like this add(new <? extends Bird>()) which obviously is not a valid syntax
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic