aspose file tools*
The moose likes Java in General and the fly likes A curious case of instanceof keyword usage in JDK 1.5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "A curious case of instanceof keyword usage in JDK 1.5" Watch "A curious case of instanceof keyword usage in JDK 1.5" New topic
Author

A curious case of instanceof keyword usage in JDK 1.5

Prince PV
Greenhorn

Joined: Jan 29, 2009
Posts: 5
Hello,

As most of us know the instanceof keyword can be used to test if an object is of a specified object type. And the syntax for its common usage is as follows -
if (objectReference instanceof type).

Recently i came across a code piece on which the instanceof keyword returns an unexpected result. My The piece of code is following.

class Cat { }
class Dog {
public static void main(String [] args) {
Dog d = new Dog();
System.out.println(d instanceof Cat);
}
}


The weird part about the code is instead of returning false, the instanceof keyword triggers a compile error with the following message -

Dog.java:5: inconvertible types
found : Dog
required: Cat
System.out.println(d instanceof Cat);
^
1 error


Does anyone know -
1) why this error comes and
2) why the instanceof keyword doesnt return a false value.


Any help is very much appreciated.
Prince
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
The compiler knows there's no way d could be a Cat, so it's a compile time error.

You can try declaring d as Object (Object d = new Dog()). Your code will compile, and you'll get the "false" output, too.


SCJP 1.4 100%
SCJD 99.5%
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Prince,

Please read your private messages regarding an important announcement.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Prince PV
Greenhorn

Joined: Jan 29, 2009
Posts: 5
Ok. Does it mean objects has to be typecasted to java.lang.Object before using them with instanceof keyword.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2713
    
    5

"Prince PV " did you check the private message sent by Rob?


SCJP, SCWCD.
|Asking Good Questions|
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

Using instanceof will result in a compile error if there the two objects being compared are incompatibly different types, as in your Dog and Cat example. Only if there is a chance of the statement being true at runtime will it pass the compiler. So no, you don't have to "typecast" (maybe you meant cast?) objects before using the test, but one class has to be in the hierarchy of the other. (I believe instanceof Interface will always compile.) This make sense sort of, right? Why test for something that can't possibly be true?

(It's been awhile since I've been to this forum. Sorry if I'm breaking a new etiquette rule by answering a question before the user name issue is resolved.)
Prince PV
Greenhorn

Joined: Jan 29, 2009
Posts: 5
Greg, Thanks for the clarification. The issue is cleared now. My conclusions are the following -

1) Due to the compile time comprison check, the instanceof keyword returns true by default.
2) Comparison with incompatible types can be done only after typecasting the reference variable to a common class in the inheritance heirarchy.

Thanks a lot
Prince
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
But you are still not complying with the naming policy.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Greg Charles wrote:Sorry if I'm breaking a new etiquette rule by answering a question before the user name issue is resolved.
A lot of us prefer not to reply to somebody with a name issue, but it isn't actually a rule.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

1. instanceof doesn't return true by "default". I'm not sure what you mean by that.

2. Comparing incompatible types is a waste of time, so the compiler assumes you made a mistake. Referring to a Dog object with an Object reference doesn't involve a cast. (Object o = new Dog(); See? No cast.) However, o might refer to a Cat object later in the code, so the compiler allows the instanceof check to compile.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A curious case of instanceof keyword usage in JDK 1.5
 
Similar Threads
A curious case of instanceof keyword usage in JDK 1.5
instanceOf Compiler error
instanceof
I'm not understanding why the instanceof operator results in compilation error
Reg. casting from Khalid Mughal