• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

UnBounded - Generics

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I was practicing unbounded wildcard. I tried the below program and i am getting the error as follows.



Error:
LogHelper.java:23: error: no suitable method found for add(Integer)
n.add(new Integer(5));
^
   method Collection.add(CAP#1) is not applicable
     (argument mismatch; Integer cannot be converted to CAP#1)
   method List.add(CAP#1) is not applicable
     (argument mismatch; Integer cannot be converted to CAP#1)
 where CAP#1 is a fresh type-variable:
   CAP#1 extends Object from capture of ?
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

But i tried the same with the below program by passing the list to a method that uses wildcard and its working.  Can you please help me understand the same.



Thanks in advance!!!
 
Saloon Keeper
Posts: 13500
305
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first program you're trying to add Integer to List<?>.

In the second program you're getting Object out of List<?>.

So you didn't do the same thing.
 
Bartender
Posts: 370
44
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java Tutorials have a good explanation of this, you can read up on wildcards here.

To zoom in on just one section, it gives an example like yours and explains what the behaviour is:

The Java Tutorials wrote:Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error

Since we don't know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection. When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.

On the other hand, given a List<?>, we can call get() and make use of the result. The result type is an unknown type, but we always know that it is an object. It is therefore safe to assign the result of get() to a variable of type Object or pass it as a parameter where the type Object is expected.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic