aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof operator" Watch "instanceof operator" New topic
Author

instanceof operator

madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
hi all!
can anyone tell me a good link
for understading instanceof operator
thanx in advance.
madhur
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What have you read so far? Have you tried a few examples? What don't you understand?
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
The instanceof operator gives you the runtime type of an object pointed to by an object reference. It has 2 arguments
objectReference instanceof ClassName
In the code below, I tried generating random instances and tried to determine the runtime type using instanceof. I used getClass() to verify the results:

It doesn't have to be the exact runtime type. A superclass type will return true as well (this was also demonstrated in the program above). That is, if the object reference x referred to an object of type SubClass,
x instanceof SuperClass
will return true as well, since, by inheritance, a SubClass object is a SuperClass object as well ("a cat is also a mammmal"). This is also true for interface types
x instanceof SomeInterface
will also return true since SomeInterface is a supertype of x. The code above implies this, since the return value of generateInstance does not throw a ClassCastException.
madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
hi!
thanx for the example.
in the whoIAm() method of PeerClass suppose i add
this instanceof SubClass
then compile error results.
but when i write in main()

SomeInterface i=new PeerClass();
System.out.println(i instanceof SubClass);
there is no error
why?
madhur.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
You can read about instanceof operator in JLS 15.10.20.2

in the whoIAm() method of PeerClass suppose i add
this instanceof SubClass
then a compile error results.

Because PeerClass isn't "hierarquically" related to Subclass. That is, nor PeerClass is a supertype of SubClass, neither SusClass is a supertype of PeerClass.

but when i write in main()
SomeInterface i=new PeerClass();
System.out.println(i instanceof SubClass);
there is no error

Because SomeInterface is a supertype of SubClass.
Notice that the compiler will complain in the same manner as it would do with a cast expression.
Also the compiler would only check the compile type of the expressions, not the runtime type.
I like to call it the declared type. The declared type of the variable i is SomeInterface.
The declared type of this within a instance method is the class where the method is. In our example the declared type of this is PeerClass.


SCJP2. Please Indent your code using UBB Code
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi There: I changed following statement:
SomeInterface i = (SomeInterface) generateInstance();
to
SomeInterface i = generateInstance();
Still I am getting same results. So can we safely conclude that instanceof provides the original type of the object when created. It does not report declared type or converted type.
Thanks
Barkat
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
You are right Barkat.
However your example doesn't change anything because the declared type of i in "SomeInterface i = generateInstance();" is still SomeInterface.
Think of "variable instanceof Type" as you would do of "(Type) variable" regarding the following:
a) The compiler will complain if the declared type of variable (aka compile type) isn't "hierarquically" related to Type.
b) At runtime both find out the type of the object pointed by the variable (aka runtime type)
[ September 02, 2002: Message edited by: Jose Botella ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
When deciding whether instanceof or a cast is legal, the compiler does not remember anything about the value that you most recently assigned to a variable.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof operator