• 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

Query related to instanceof operator behaviour at compile time

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

As per my knowledge, the instanceof operator compiles only if the reference type compared to class type are in the same inheritance tree.
According to that, below code should not compile but it compiles FINE!!.
Please correct me if I am wrong and please help me for the reason.

Output:No output generated since the if condition fails
 
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rohan,

I believe you are confused about compile time and run time.
instanceof is a run time operator, that most of the time can be replace by overloading but that's another discussion.

Imagine a method like:


It's a stupid example but it shows why you can't process instanceof at compile time.

Joe
 
Rohan Ramchander
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,

In your code, the method showClass(Object o) takes input as Object therefore it can be compared with any Class types (whether A or B).

But below code will NOT compile:
[code=Java]
class Cat{}
class Dog{

public static void main(String arg[]){

Dog d = new Dog();
System.out.println(d instanceof Cat);

}
}
[\code]

error is:
C:\java_aug2013>javac Dog.java
Dog.java:7: error: inconvertible types
System.out.println(d instanceof Cat);
^
required: Cat
found: Dog
1 error
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason your first example is different is that Inter is an interface. So you're checking whether an instance of that interface is also an instance of Someone.

Can you see that it's possible for this to be true? There is no way an instance of a Dog can be a Cat, but if we create a subclass of Someone that implements Inter then we can have an object that is an instance of both.

And welcome to the Ranch!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Ramchander wrote:
As per my knowledge, the instanceof operator compiles only if the reference type compared to class type are in the same inheritance tree.
According to that, below code should not compile but it compiles FINE!!.




I don't know how you came to the conclusion.... Yes, if two classes are not in the same inheritance tree, then it is not possible for the cast to succeed, hence, the compiler complains. However, it isn't obvious for interfaces, so you can't draw the conclusion that casting between interfaces is not possible, when two interfaces are not in the same inheritance tree.

Henry
 
Rohan Ramchander
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

Now I can understand that!!!

1. In Dog and Cat Example, Dog or sub-types of Dog can never be a Cat since Class can extend only one Class.
Therefore compilation fails.

2. But in InstanceOfTest Example, there can be a class which is sub-type of Someone and also implements Inter.
Therefore code need to compile since Compiler never knows what object is being created at runtime

 
reply
    Bookmark Topic Watch Topic
  • New Topic