• 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

Instanceof in java

 
Ranch Hand
Posts: 30
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys.......

I want to know what is instanceof in java?

I googled it but i can't find a correct and simple def for it.

And also i want to know what is the usage of Instanceof in normal application.



Thanks in advance
 
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Varadhan Sesharaman wrote:
I googled it but i can't find a correct and simple def for it.



Am surprised

instanceof is an operator used to check whether a particular reference variable(or instance variable) is of the type of a particular class. The instanceof check returns either a true or a false.

Example :
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Varadhan Sesharaman wrote:
And also i want to know what is the usage of Instanceof in normal application.


for instance, look java.lang.String#equals(Object)
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar M K wrote:

Varadhan Sesharaman wrote:
I googled it but i can't find a correct and simple def for it.



Am surprised

instanceof is an operator used to check whether a particular reference variable(or instance variable) is of the type of a particular class. The instanceof check returns either a true or a false.

Example :



Good explanation. Except for the fact that



won't even compile since they are not in the same inheritance tree.
 
Varadhan Sesharaman
Ranch Hand
Posts: 30
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for explaining in a simplw way.


I got the point now.



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Varadhan Sesharaman wrote:
I got the point now.


 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you have that sort of question, you should look in the Java Language Specification, where you find it tests whether

the reference could be cast (ยง15.16) to the ReferenceType without raising a ClassCastException.

It returns false if you put null as its left-hand operand. The right-hand operand can be a class name or an interface name.

Its use in normal programming: as little as possible.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Pouwels wrote:


won't even compile since they are not in the same inheritance tree.



Oops! Thanks for the correction!
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic