• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

A curious case of instanceof keyword usage in JDK 1.5

 
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
 
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.
 
Sheriff
Posts: 21760
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Prince PV " did you check the private message sent by Rob?
 
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • 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
 
Marshal
Posts: 64707
226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you are still not complying with the naming policy.
 
Campbell Ritchie
Marshal
Posts: 64707
226
  • 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: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • 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
Boost this thread!