• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instanceof in java

 
Varadhan Sesharaman
Ranch Hand
Posts: 30
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 :
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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)
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varadhan Sesharaman wrote:
I got the point now.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic