5

I was going through wildcard topic in Java where i got stuck at this code below

  static <T> void type(List<? super T> list){
      //...
  }
  static <T> void type2(List<? extends T> list){
      //...
  }

If you call these methods by this code, it gives an error and that's understandable

List<?> unbounded=new ArrayList<Long>();
//! type(unbounded); //:Error here
//! type2(unbounded); //:Same Error here

But if you change signature of these methods by adding one extra arg, like this

  static <T> void type(List<? super T> list, T arg){
      //...
  }
  static <T> void type2(List<? extends T> list, T arg){
      //...
  }

and call them by

List<?> unbounded=new ArrayList<Long>();
Long lng=90L;
//! type(unbounded,lng); //:Error here
type2(unbounded,lng); //No error in this one now

Why this behavior just by adding one extra arg?? How come bounded list accept unbounded one just by addition of one xtra arg??

abhi_awake
  • 186
  • 1
  • 8
  • So sorry for that.... Updated as necessary – abhi_awake Mar 03 '18 at 14:08
  • You should not see an error in the first code block. Also, `Long lng=90;` should be `Long lng=90L;`. Please provide a [MCVE]. (I think it's even simpler for *you*, and even more for potential answerers...) – Marco13 Mar 03 '18 at 14:12
  • Whoops: The first block **does** cause an error, but only in `javac`, and not in Eclipse. Surprising, indeed (but likely caused by the old Eclipse version that I've used here...) – Marco13 Mar 03 '18 at 14:14
  • Although, admittedly, right now, I'm not entirely sure whether `javac` is wrong or the Eclipse compiler is wrong. +1 for that. – Marco13 Mar 03 '18 at 14:18
  • @Marco13 Can you please explain what is happening here. Is `T` here `List extends T> list=unbounded` changing to `Object`. So then according to you it must not cause an error. – abhi_awake Mar 03 '18 at 14:25
  • It would be helpful if you can how you are running this? Compiler, java version? – Thiyagu Mar 03 '18 at 14:33
  • About the first 2 errors, logically those should both work if `T = Object`. I can reproduce with javac in jdk 8, but it compiles fine with jdk 9. So it seems like there was a type inference change or bug that was fixed. – Jorn Vernee Mar 03 '18 at 14:38
  • on Netbeans 8.2 Java 1.8.0 – abhi_awake Mar 03 '18 at 14:38
  • 1
    No, not `T = Object`, sorry. Eclipse is inferring `T` to the the same type as the capture. `T = Object` only works for the `? extends T` version. – Jorn Vernee Mar 03 '18 at 14:47
  • yes that's correct `T=object` is only working for `? extends T`. In `? super T` it is giving error `Incompatible types: List cannot be converted to List super Object>` – abhi_awake Mar 03 '18 at 14:50
  • I **think** this is a duplicate of https://stackoverflow.com/questions/38246476/lower-bounded-wild-card-causes-trouble-in-javac-but-not-eclipse , but this was only one of the first search results that *could* match. A more detailed analysis may still be worthwhile. – Marco13 Mar 04 '18 at 02:38
  • @Marco13 excellent find, +1 being a duplicate – Eugene Mar 04 '18 at 07:32

0 Answers0