Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )

 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic