• 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

instanceof operator  RSS feed

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why is it giving compiler error instead of printing false?
Thanks
Barkat
[ June 26, 2003: Message edited by: Barkat Mardhani ]
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


JLS 15.20.2
RelationalExpression instanceof ReferenceType
If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.


The left-hand side reference must be cast-compatible with the right side. If you tried to force a cast

You would get a compile error because there is no inheritence relationship between the two.
Although, I noticed a way to get around it.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone give an example that shows instanceof expression will result in false?
[ June 26, 2003: Message edited by: Barkat Mardhani ]
 
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object b = new JButton();
System.out.println(b instanceof String);
also:
JButton jb = null;
System.out.println(jb instanceof JButton);
[ June 26, 2003: Message edited by: Thomas Paul ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. So can I say following about instanceof operator:
Compile time type of left operand should be Object or a type to which righ side operand can be casted to.

Thanks
Barkat
[ June 26, 2003: Message edited by: Barkat Mardhani ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instanceof requires that it is possible that the statement might be true. So you must be able to cast the left side object to be a right side object.
In my first example casting b to be a String will compile although it will cause a runtime exception. Therefore the instanceof is permitted.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some examples where instanceof returns false.

false
false
false
false
false
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barkat, in order NOT to get compiler errors when using instanceof, you must have both classes you are dealing with in the SAME inheritence hierarchy.
In your example A and B are NOT in the same hierarchy. They just both of them extends Object. And that is why if you modify a bit the code, to make use of Object, than you "trick" the compiler. However by trying to cast, you get definately a runtime ClassCastException.
e.g.
b instanceof A // compiler error as you suggested
((Object) b) instanceof A // NO compiler error;((Object) b) is an reference of type Object and it is in the same hierarch with A

If in your example, you let class B extend class A, then any instanceof tests are allowed (between A and B) since they are in teh same hierarchy.
e.g.
class A {}
class B extends A {}
// form a test program
Test() {
A a = new A();
A b = new B();
System.out.println(a instanceof B); // compiles (A and B are in the same hierarchy) but returns false(since a can not be casted to B)
System.out.println(b instanceof A); // compiles and returns true (since b can be casted to A)
}
public static void main(String[] args) {
new Test();
}
I hope it helps.
Miki
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!