• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Regarding instanceof operator

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anybody please explain me about instanceof operator,how it is used etc?

Thanks in advance

Sneha
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sneha Kulkarni:
Can anybody please explain me about instanceof operator,how it is used etc?



"instanceof" operator is used to determine the object type at runtime.

You can take a decision based on the actual object so as to invoke its functionality.

A sample code would be:



Note:
  • You can use only OBJECTS to check using instanceof operators as the term 'instance' indicates


  • you can use the fully qualified classname in the checking as 'java.lang.String' instead of 'String'. This will be helpful to use the classes-happen-to-have-same-name (for example, java.awt.List and java.util.List)


  • the instanceof operator returns true for the object when compared against any of its compatible and/or super class(es). Any object when compared against java.lang.Object would return true always


  • Does that help you?
    [ September 10, 2007: Message edited by: Raghavan Muthu ]
     
    Sneha Kulkarni
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Raghavan Muthu:


    "instanceof" operator is used to determine the object type at runtime.

    You can take a decision based on the actual object so as to invoke its functionality.

    A sample code would be:



    Note:

  • You can use only OBJECTS to check using instanceof operators as the term 'instance' indicates


  • you can use the fully qualified classname in the checking as 'java.lang.String' instead of 'String'. This will be helpful to use the classes-happen-to-have-same-name (for example, java.awt.List and java.util.List)


  • the instanceof operator returns true for the object when compared against any of its compatible and/or super class(es). Any object when compared against java.lang.Object would return true always


  • Does that help you?

    [ September 10, 2007: Message edited by: Raghavan Muthu ]

     
    Sneha Kulkarni
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Raghav

    If you dont mind,can you please explain me in brief?

    Thanks
    Sneha
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yeah sure.

    Please try to look at the sample code given below.

    Tell me whether this code helps you to get the information?



    The output it gives is as follows:



    If you look at the shout() method implementation code in the AnimalImpl class,
    you may get the practical usage of "instanceof" operator.

    Hope it helps to clear the doubt.
    [ September 11, 2007: Message edited by: Raghavan Muthu ]
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Perhaps, the following links may help you to get further ideas.

  • Prefer polymorphism over instanceof and downcasting, a good article explains about Polymorphism Vs instanceof.


  • Have a look at "instanceof definition" and "instanceof examples" section in this page


  • Using the instanceof keyword in Java, an article from devx.com.

  •  
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Another simple example is here



    The output is:

     
    Sneha Kulkarni
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Raghavan

    Thank you!,i understood the instanceof now.

    Regards
    Sneha
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic