Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

instanceof

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain the difference between
instanceof
and
instanceOf()
What do they do??
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never seen instanceOf()
But instanceof is an operator (not a method).
Here's what Peter ver der Linden says about in in "Just Java 2"
" The instanceof opertator is used with superclasses to tell if you have a particualr subclass object. For example we may see the following:
class vehicle{....
class car extends vehicle{ ...
class convertible extends car{ ....
vehicle v: ...
if (v instanceof convertible) ....
The instance of operator is often follwoed by a statement that casts the object from the base type to the subclass, if it turns out that the one is an instance pf the other. Before attempting the cast, instanceof lets us check that it is valid. "
Seems to me that one of the most useful things about this operator is that it can help you avoid classCastExceptions.
i've looked for something on instanceOf(), but can't find it ... what class is it a method for?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
I too never heard about instanceOf() as a method. Will keep looking. The following is what I found close to what you are looking for. Anybody please give more insight on this matter ?
class java.lang.Class
{
public boolean isInstance(Object obj) {}
/*
Determines if the specified Object is assignment-compatible
with the object represented by this Class.
This method is the dynamic equivalent
of the Java language instanceof operator.
/ }

Thanks
ARSKumar
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i'm beginning to think Shashank has got the two mixed up.
isInstance() is a method on the Class class. The API defines it as
Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.
Specifically, if this Class object represents a declared class, this method returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses); it returns false otherwise. If this Class object represents an array class, this method returns true if the specified Object argument can be converted to an object of the array class by an identity conversion or by a widening reference conversion; it returns false otherwise. If this Class object represents an interface, this method returns true if the class or any superclass of the specified Object argument implements this interface; it returns false otherwise. If this Class object represents a primitive type, this method returns false.
Parameters:
obj - the object to check
So the two seem to essentially do the similar things (ie, check that the object is an instance of a particualar type)
 
ARS Kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the following from
http://developer.java.sun.com/developer/technicalArticles/DataTypes/ObjectsConscious/index.html
To get a runtime equivalent of operator instanceof, you check both isKindOf and, if the type is an interface, check to see if obj implements that interface using a method, behavesLike, which is defined in the next section:

public static boolean
instanceOf(Object obj, String type)
throws ClassNotFoundException {
Class tClass = Class.forName(type);
if ( tClass.isInterface() ) {
return behavesLike(obj, type);
}
else {
return isKindOf(obj, type);
}
}

Now I am confused. Here InstanceOf is declared as a method.
instanceOf(Object obj, String type)
throws ClassNotFoundException ------ any explanations please...
Thanks
ARSKumar
 
ARS Kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends
Sorry for bringing this again. If anybody can clarify this I will be really thankful.
Thanks
ARS Kumar
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
instanceof is a java keyword that tells you if an object is an instance of a particular type. It returns a boolean. instanceof is used for run time type identification of the object.
e.g if(x instanceof animal){
((Horse) x).runsFast();
Here "if" checks if x is an instance of animal before casting it to aHorse. Note that instanceof is used before casting x to a Horse otherwise ClassCastException is encountered.
Hope this helps !!
Regards,
Milind

[This message has been edited by Milind (edited May 31, 2000).]
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ARS Kumar,
As you know Java language is case sensitive. instanceof is DIFFERENT from instanceOf. In java instanceof is an operator. The behaviour of instanceof operator is to find out the type of a ref at RUN TIME. The skeleton to use this operator is
objref instanceof type
In the link you pointed out, they are simulating the behaviour of the operator instanceof through writing a method called 'instanceOf' taking the lefthand and righthand operands of our Java instanceof operator as 2 arguments.
I think in order to explain what's really happening when using instanceof operator, instead of explaining through plain words they have choosen this way.
In Java language as such there is no 'instanceOf(..)' method. Only instanceof operator. We can always write any no of methods with atleast ONE letter case difference, even named as the keywords. Here in your link this method is just one of those methods which happened to explain the functionality of the operator instanceof. Is this clear to you now Kumar?
regds
maha anna
 
ARS Kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maha Anna
Thanks a lot. It is very clear to me now. Thanks for noticing my point.
ARS Kumar.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic