List l = new Arraylist(); if you use this line in your code what will we get?
-
Compilation error.
-
Runtime error
-
Compiles Fine
-
Depends on the code where you use
This code has a type mismatch - List expects Object elements, but ArrayList can only contain Strings. In Java generics, you cannot assign a more specific generic type (ArrayList) to a more general reference (List) because it would allow type violations at compile time.
This code has a generic type mismatch: 'List l' declares a reference of type List, but it's being assigned 'new Arraylist()' — besides 'Arraylist' not even being valid capitalization for java.util.ArrayList, generics in Java are invariant (ArrayList is not a subtype of List), so the assignment fails type-checking. This mismatch is caught by the compiler, not at runtime, producing a compilation error rather than compiling fine or throwing a runtime exception.