0

Consider below code snippet

List<Double> doubles = new ArrayList(List.of("1"));
doubles.add(1.0);
for (Double item: doubles) {
  System.out.println(item.intValue());
}

Java compiler fails to detect the wrong type in ArrayList and causing runtime error.

Can anyone explain why can't java compiler detect this error in compile time?

HKIT
  • 628
  • 1
  • 8
  • 19
  • 1
    `new ArrayList(…)` creates a *raw* type. You should use `new ArrayList<>(…)` instead. If your compiler didn’t give you warnings about this, you should check the compiler settings. – Holger Apr 28 '23 at 08:25

0 Answers0