0

This code is legal in Intellij Idea but illegal in eclipse

private static void gogo()
{
    List<Double> l = cc(new ArrayList());
}
private static List<String> cc(List<Integer> coll)
{
    return new ArrayList<>();
}

this is an obvious mistake but idea can compile the code without any error.

but in eclipse it's a compile error:

Multiple markers at this line

  • Type mismatch: cannot convert from List<String> to List<Double>
  • ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized
  • Type safety: The expression of type ArrayList needs unchecked conversion to conform to List<Integer>

if the code is this

private static void gogo()
{
    List<Double> l = cc(new ArrayList<>());
}
private static List<String> cc(List<Integer> coll)
{
    return new ArrayList<>();
}

the intellij idea will tell me the code is error

Incompatible types.

  • Required: List<java.lang.Double>
  • Found: List<java.lang.String>

the difference of two code is the <> after ArrayList.

S.Lee
  • 206
  • 1
  • 4
  • 15
  • why would this be a mistake? also: It's Java that decides what is "legal", not the IDE you use – Stultuske Nov 21 '18 at 06:47
  • @Stultuske an assignment of a `List` to a `List` variable should give a compile error. – Henry Nov 21 '18 at 06:50
  • @Henry "should"? No. Would be nice if if it gave one, perhaps, but that's not what the spec says it should do. – Andy Turner Nov 21 '18 at 07:01
  • @Stultuske do you have a reference? For example `List l = new ArrayList();` gives an error. – Henry Nov 21 '18 at 07:04
  • @Henry https://ideone.com/V7OIxf – Andy Turner Nov 21 '18 at 07:14
  • @AndyTurner Yes, but it is still puzzling that the raw actual value for the parameter `coll` has influence on the return type of the method `cc`. – Henry Nov 21 '18 at 07:17
  • @AndyTurner This code in eclipse can't be compiled. The message is Type mismatch: cannot convert from List to List – S.Lee Nov 21 '18 at 07:35
  • @S.Lee as I said: it's Java (or better: the compiler used) that decides this, not the IDE – Stultuske Nov 21 '18 at 08:06
  • Eclipse is the one that is broken here. [The language spec says](https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-15.12): "If the chosen method is not generic, then: If unchecked conversion was necessary for the method to be applicable, the parameter types of the invocation type are the parameter types of the method's type, and the return type and thrown types are given by the erasures of the return type and thrown types of the method's type." – Andy Turner Nov 21 '18 at 08:20
  • @AndyTurner Indeed, I settled down on the same paragraph in 15.12.2.6 of the JLS. – Henry Nov 21 '18 at 08:25

0 Answers0