• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Q about instanceof

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code is from Sierra&Bates book. It would not compile. The error message says �inconvertible types.� I donot know why one can not check whether t is an object of type String. Sorry I do not understand such fundamental stuff.

Edited by Corey McGlone: Added CODE tags
[ February 25, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole point of the instanceof operator is to determine the "runtime type" of an object. This becomes an issue because, with inheritance, you can use a child object anywhere a parent is expected. Therefore, the following is legal:

You see, we are assigning an Animal reference to a Cat object. This is perfectly legal because a Cat "is a" Animal.
However, a can not reference just any old class. For example, you can't reference a String object because a String "is not a" Animal. The following would give you a compiler error:

You're getting an error message because you're testing to see if an object is of a type that it can't possibly be. There is no way that the object referenced by t could be a String object because String does not extend Ticker. In short, String "is not a" ticker.
That's why you're getting the error message. I hope that helps,
Corey
P.S.
I realize you're new to the forums, Kathy, but I would appreciate if you would insert "CODE" tags around your code when you post some. It helps retain the formatting which makes it much easier to read. You can check out this page for details about how to use UBB tags in your posts and there is also a series of buttons beneath "Instant UBB Code" that will help you insert proper tags.
If you can put those tags in there to begin with, you not only reduce my workload (I won't have to edit your posts), but you'll also make it easier for other people to understand and respond to your posts.
Thanks.
[ February 25, 2004: Message edited by: Corey McGlone ]
 
Kathy Cai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey,
Thanks. I will start to use CODE" tags.
Thank you for answering my question. But this time I did not get the point well. It seems instanceof has very limited usage. Say, if the RHS of istanceof is class Animal, I can not put an arbitrary object on its LHS. For example, I can not put �t� an object of String type there. since (t instanceof Animal) is not allowed. This means when I write the code I have to know the object to be tested must be from either the Animal class or its superclass / subclass.
I guess I do not exactly know the intention of instanceof.
Kathy
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to know before hand that the operands used in the instanceof operator belong to the same class/class hierarchy/implement same interface somewhere up in the hierarchy but you want to test which class. Maybe this example clears the idea for you:


Please ignore those comments I have been reading the book by Kaythy and Bert a lot these days and I am tired of driving my Mazda Protege.
I hope that helps
Sindhur.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Kathy Cai:
It seems instanceof has very limited usage. Say, if the RHS of istanceof is class Animal, I can not put an arbitrary object on its LHS. For example, I can not put �t� an object of String type there. since (t instanceof Animal) is not allowed. This means when I write the code I have to know the object to be tested must be from either the Animal class or its superclass / subclass.
I guess I do not exactly know the intention of instanceof.

If you look at the JLS, §15.20.2 Type Comparison Operator instanceof, you see the following text:

"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."

You see, you should NEVER have to test to see if an object referenced by a String object is really an Animal - you already know that it can't be. What would be the point of such a test? You might as well write "if (false)".
Where this operator can come in very handy is in the case of overriding methods. Take, for example, the equals(Object) method defined in java.lang.Object. Let's look at an example:

In this case, we've defined a class named Animal which has a member named "id." If any two animals have the same id, we consider them equal. In order to get them to compare that way, we need to override the equals method defined in java.lang.Object. However, that method takes an arbitrary Object as a parameter. Therefore, it would be quite possible to pass a String object to this method.
In order to do the comparison, however, we need to get the value of the id member of the passed object. Object, however, doesn't define a member named id so we must first cast the object (o) as an Animal. However, before we do that cast (and risk a ClassCastException), we should check to make sure that the object referenced by o really IS an Animal. Therefore, we first use the instanceof operator to ensure that our upcoming cast won't produce an exception.
I hope that helps clear up the issue for you. If you're still confused or have other questions, please let me know.
Corey
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I came across this question
Which of the following statements are true?
1) The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface.
2) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class
3) The instanceof operator will only determine if a reference is an instance of a class immediately above in the hierarchy but no further up the inheritance chain
4) The instanceof operator can be used to determine if one reference is of the same class as another reference thus
According to me no option is correct, but to my surprise the answer is 2.
Please Help
Regards
Rashi
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why wouldn't number 2 be correct? This is an example of what #2 is saying:

That's really what that question is asking about. Is there something else that is confusing you about this?
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your program to compile you should change it to
CODE
import java.awt.*;
class Ticker extends Component {
public static void main (String [] args) {
Ticker t = new Ticker();
boolean test = ((Object)t instanceof String);
}
}
CODE
As explained in the earlier posts, Compiler is smart enough to see the relation between the LHS and RHS operands. If it sees no relation can be established even during the run time, it won't let you get away with that and screw up your own code
These are some of the great qualities of Java.
 
reply
    Bookmark Topic Watch Topic
  • New Topic