aspose file tools*
The moose likes Beginning Java and the fly likes Instanceof  in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instanceof  in java" Watch "Instanceof  in java" New topic
Author

Instanceof in java

Varadhan Sesharaman
Ranch Hand

Joined: Apr 21, 2011
Posts: 30

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

Joined: Jul 03, 2011
Posts: 256
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Feb 22, 2012
Posts: 61
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

Joined: Apr 21, 2011
Posts: 30

Thank you guys for explaining in a simplw way.


I got the point now.



Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Varadhan Sesharaman wrote:
I got the point now.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Jul 03, 2011
Posts: 256
Jim Pouwels wrote:


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


Oops! Thanks for the correction!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instanceof in java