• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between cannot be applied to and cannot find symbol (method)  RSS feed

 
Tapio Niemela
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!