Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A curious case of instanceof keyword usage in JDK 1.5

 
Prince PV
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prince,

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
Prince PV
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Does it mean objects has to be typecasted to java.lang.Object before using them with instanceof keyword.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Prince PV " did you check the private message sent by Rob?
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48980
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you are still not complying with the naming policy.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48980
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic