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.

Sachin Sachdeva
  • 10,744
  • 2
  • 45
  • 108

0 Answers0