| Author |
Problem with instanceof
|
Prashanth Lingala
Ranch Hand
Joined: Nov 13, 2004
Posts: 66
|
|
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
|
Have A Nice Day !!!
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24041
|
|
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 ]
|
[Jess in Action][AskingGoodQuestions]
|
 |
Prashanth Lingala
Ranch Hand
Joined: Nov 13, 2004
Posts: 66
|
|
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
Joined: May 15, 2001
Posts: 673
|
|
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
Joined: Nov 13, 2004
Posts: 66
|
|
I appreciate your responses , but i still am clueless??? can you further simplify it...
|
 |
Prashanth Lingala
Ranch Hand
Joined: Nov 13, 2004
Posts: 66
|
|
I appreciate your responses , but i still am clueless??? can you further simplify it...
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
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
|
Java API Documentation
The Java Tutorial
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24041
|
|
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.
|
 |
 |
|
|
subject: Problem with instanceof
|
|
|