What is raw type in Java Generics and why should they be avoided?
Hi everyone! I'm a beginner Java developer, learning generics and came across a strange concept "raw type". I think I understand that it's related to generics, but I don't fully grasp the essence.
Let's say I have this code:
```java
List list = new ArrayList(); // is this a raw type?
list.add("string");
list.add(123);
```
The compiler gives a warning, but the code works. I can put anything into the list. But if I use a parameterized type:
```java
List list2 = new ArrayList<>();
list2.add("string");
// list2.add(123); // compilation error
```
Then the compiler complains. What's the difference? Why is raw type considered bad practice? After all, the code works with it. I've heard that raw types exist for backward compatibility, but why use them in new code?
No answers yet.
Log in to leave an answer