7

Following code works fine while it should not compile.

I am not sure why assignment of a List to String is permitted.

It only happens to List interfaces. Changing to a Array List working fine as expected and it no longer compiles.

public class Test {
        public static <T extends List<Number>> T newList() {
            return null;
        }

        public static void main(String[] args) {
            String s = newList();
        }
    }

Please help me understand this behavior.

T-Bag
  • 10,916
  • 3
  • 54
  • 118

0 Answers0