0

Java newbie here. Can someone explain to me why the following code snippets behaves as shown below

var list1 = List.of(); //Line 1 --> creates a List<Object>

var list2 = List.of("A"); //Line 2 --> creates a List<String>

list1 = list2; //Line 3--> compiler error because List<Object> is not a List<String>

However,

List list1 = List.of(); //Line 1 --> creates a List<Object>

var list2 = List.of("A"); //Line 2 --> creates a List<String>

list1 = list2; //Line 3--> no compiler error why??

Thanks

Pshemo
  • 118,400
  • 24
  • 176
  • 257
  • 1
    `List` is a raw type. You get no generic type checking with it, and it's a very bad idea to use a raw type. – Kayaman Jan 09 '22 at 17:21

0 Answers0