• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with instanceof

 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following:

interface A {}
class X implements A {}
class Y extends X {}

Y[] instanceof Y should give me false
Y[] instanceof X should give me false
Y[] instanceof A should give me false

Y[] instanceof Object should give me true

Y[1] instanceof A should give me true
Y[1] instanceof X should give me true
Y[1] instanceof Y should give me true
Y[1] instanceof Object should give me true

This is what the book suggests, and i trust it...

but when i do the following:
=================================================================


i get the following Error:
---------- Javac ----------
Test.java:8: '.class' expected
System.out.println(Y[] instanceof Y);
^
Test.java:8: ')' expected
System.out.println(Y[] instanceof Y);
^
2 errors
Normal Termination
Output completed (0 sec consumed).
=================================================================
Can somebody help me only with one line of code,
i mean i dont know how to get the true value
Also explain me where am i making mistake???

Thank You for your time...

Regards
Prashanth Lingala
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The left hand argument to "instanceof" is an object, not a class. You use instanceof to ask if a specific object is an instance of a given class or interface. So you have to actually create a Y[], and use that object:

Y[] array = new Y[1];
array[0] = new Y();
System.out.println(array instanceof Y); // false
[ February 16, 2005: Message edited by: Ernest Friedman-Hill ]
 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i get the following error:
---------- Javac ----------
Test.java:10: inconvertible types
found : Y[]
required: Y
System.out.println(array instanceof Y);
^
1 error
Normal Termination
Output completed (0 sec consumed).


Thank You For Your Time

Regards
Prashanth Lingala
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prashanth Lingala:
Given the following:

interface A {}
class X implements A {}
class Y extends X {}

Y[] instanceof Y should give me false
Y[] instanceof Object should give me true

Y[0] instanceof Y should give me true
Y[0] instanceof Object should give me true



I have edited a quote from your first query, which contained the answer to your question.
 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your responses , but i still am clueless???
can you further simplify it...
 
Prashanth Lingala
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your responses , but i still am clueless???
can you further simplify it...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the original post, you said "Y[] instanceof Y should give me false". However, this is not exactly true as you can see by the compiler error you get. The compiler is smart enough to tell that there is no possible way for this statement to return true because Y[] and Y are not related by inheritence in any way. This is why you get a compiler error.

Perhaps, I can illustrate further with another example:

Because X and Y are related by inheritence, x1 COULD be an instance of Y. The compiler has no way to determine whether it is or not since this information is only available at run time. However, in the case of

The compiler can tell right away that x3 will NEVER be an instance of Y because there is no inheritence relationship. In this case, the compiler can give an error because all the necessary information is available at compile time.

HTH

Layne
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, I'm sorry that my explanatory example caused more problems than it solved. I might have written

Object array = new Y[1];
System.out.println(array instanceof Y); // false

By hiding the type information from the compiler, you get to force Java to answer this question at runtime.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic