difference between cannot be applied to and cannot find symbol (method)
Tapio Niemela
Ranch Hand
Joined: Jan 06, 2006
Posts: 69
posted
0
Hello fellow javaranchers.
I'm puzzled what is the difference between "cannot be applied to" and "cannot find symbol" (method)
I understand simple case where "cannot find symbol" occurs due to misspelling of the method name or similar (like calling System.print instead of System.out.print), but http://www.javafixer.org/cannot-find-symbol.php suggests that following situations could lead to "cannot find symbol" compilation error:
-An extra parameter was added or was left out.
-The parameters are used in the wrong order.
However those situations will end in "cannot be applied to" -errors instead of "cannot find symbol", for example consider following code:
Also following situation is even stranger:
Any idea why adding object of wrong type to Collection gives different compile error vs. adding object of wrong type to Set (or List)? Are Set and List given *some* special care by the compiler?
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3144
posted
1
Tapio Niemela wrote:Hello fellow javaranchers.
I'm puzzled what is the difference between "cannot be applied to" and "cannot find symbol" (method)
I understand simple case where "cannot find symbol" occurs due to misspelling of the method name or similar (like calling System.print instead of System.out.print), but http://www.javafixer.org/cannot-find-symbol.php suggests that following situations could lead to "cannot find symbol" compilation error:
-An extra parameter was added or was left out.
-The parameters are used in the wrong order.
However those situations will end in "cannot be applied to" -errors instead of "cannot find symbol", for example consider following code:
I doubt there's anything official on it, other than that the compiler is required to produce errors in all these situations. The specifics of those two particular errors may vary among compiler implementations or versions, or there may be subtle differences not captured by that completely non-authoritative source you quote. For instance, I think I've seen "cannot be applied" when there's exactly one method with the given name, but a different arg list, and "cannot find symbol" where there are zero methods with that name, or more than one.
There's really no value in knowing the details though.
Also following situation is even stranger:
There's nothing strange in the compiler not giving the error messages you intuitively expect it to.
Any idea why adding object of wrong type to Collection gives different compile error vs. adding object of wrong type to Set (or List)? Are Set and List given *some* special care by the compiler?
I get "cannot be applied" for both testSet and testCollection.
Tapio Niemela
Ranch Hand
Joined: Jan 06, 2006
Posts: 69
posted
0
Hello Jeff and thanks for the answer
Yes it seems which error is given is compiler-specific. I was once again studying generics and ran into this "issue"..What I also found was that on my compiler (1.6.0) I got "cannot find symbol, if SubClass has ALSO defined generics method", which is the case for List.add(E e) and Collection.add(E e)
This is what I found out (if anyone is interested with details )
subject: difference between cannot be applied to and cannot find symbol (method)