Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes Generics - type argument inference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics - type argument inference" Watch "Generics - type argument inference" New topic

Generics - type argument inference

Johnny Augustus

Joined: Oct 07, 2006
Posts: 18

I have a doubt with the following code snippet (TIJ by Bruce Eckel)

Why is it that the second call to writeExact() in the static f1 method marked as an error? I have tried this program out and it is be working fine.

As far as I know, the compiler should be able to figure out the type of parameter from the call 'writeExact(fruit, new Apple())' as Fruit and hence work properly.

Following is the explanation given,
"The writeExact() method uses an exact parameter type (no wildcards). In f1() you cn see that this works fine - as long as you only put an Apple into a List<Apple>. However, writeExact() does not allow you to put an Apple into a List<Fruit>, even though you know that should be possible"

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Johnny,

you're right, Bruce is wrong.

If you have a method signature, where one parameter is a container of T and the other parameter is T itself, then you can invoke it with a container with the generic supertype of T and T itself.
So in your example:

the type of list can be the same type as item or a supertype.

And you also can get your apple out of the fruit list. When you change the code in the main method to

it compiles without warning and prints


all events occur in real time
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Indeed, what Bruce was getting at is that you cannot pass a List<Fruit> as if it were a List<Apple>. Parameterized types are not covariant, a List<Fruit> is by definition not a List<Apple>. In your example my compiler is smart enough to infer the type as Fruit and not Apple so there is no warning. If a compiler were to infer the type is Apple then you'd have some problems.
I agree. Here's the link:
subject: Generics - type argument inference
jQuery in Action, 3rd edition