This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
     
    This is my favorite tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic