• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic operands are not allowed for instanceof operateor

 
Ranch Hand
Posts: 61
Firefox Browser Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here why line two is causing error.
Can we not allow generic as a operand for instanceof operator
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Having answered your last two questions, this time I'll help you come to an answer on your own. But first tell us, why do you think the code should compile?
 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Firefox Browser Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Jain wrote:Ok. Having answered your last two questions, this time I'll help you come to an answer on your own. But first tell us, why do you think the code should compile?


Ok sir what i am thinking set is a reference of Object type having instance of TreeSet<Integer> /and TreeSet is an subclass of Navigable interface.So instance of TreeSet is also an instance of Navigable.
On my part the reason of compilation fail will be due to generic variable Integer.But i want to ask why?
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek KumarSoni wrote:On my part the reason of compilation fail will be due to generic variable Integer.But i want to ask why?


Yeah you're right. It's due to that. Why? Ok, think of when is the instanceof check done - a). At compile time, b). At runtime.
Once you understand this, then go back and see your previous question. Try to relate the answer there, and the reason you just posted. You'll get the answer.
 
Abhishek KumarSoni
Ranch Hand
Posts: 61
Firefox Browser Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R. Jain wrote:

Abhishek KumarSoni wrote:On my part the reason of compilation fail will be due to generic variable Integer.But i want to ask why?


Yeah you're right. It's due to that. Why? Ok, think of when is the instanceof check done - a). At compile time, b). At runtime.
Once you understand this, then go back and see your previous question. Try to relate the answer there, and the reason you just posted. You'll get the answer.


Sir with previous three question and your answer helps me a lot.thanks a lot for your respons to my query.
Generic is the part where i am hurted a lot.This is my week area.
Could you please explain me one more thing whar is difference between a wildcart<?> and a type parameter
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhishek KumarSoni wrote:Could you please explain me one more thing whar is difference between a wildcart<?> and a type parameter


Well, it's a bit of a broad question, but I can point out few differences in bullets:
  • Wildcards can have both upper and lower bounds, while type parameters can have only upper bounds.
  • Wildcards cannot have multiple bounds, whereas type parameters can have multiple bounds - <T extends Comparable<T> & Serializable>


  • The choice of which one to use will depend upon what you want to do. For example, if you just want a method to iterate over any Collection and print the contents, then using wildcards would suffice. However, if you are concerned about a particular type you get, or you want to enforce more type safety, you would use a type parameter. To explain on that further, a Collection<T> is a homogenous collection of a particular type, that is not known at compile tie, whereas, a Collection<?> is a heterogenous collection. That means you don't know what type you will get from it. It might contains at the same time a String, an Integer, and a Date.

    As I said, it's quite a broad topic, you would be better off going through some tutorials on generics: Here is the good place to start. Then when you want to have a detailed understanding of this topic, then I would suggest you to spend some time on Java Generics FAQs.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic