File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q about instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q about instanceof" Watch "Q about instanceof" New topic
Author

Q about instanceof

Kathy Cai
Greenhorn

Joined: Jan 13, 2004
Posts: 13
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 ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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 ]

SCJP Tipline, etc.
Kathy Cai
Greenhorn

Joined: Jan 13, 2004
Posts: 13
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
Sindhur Sat
Greenhorn

Joined: Feb 23, 2004
Posts: 23
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

Joined: Dec 20, 2001
Posts: 3271
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
Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
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

Joined: Dec 20, 2001
Posts: 3271
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?
Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
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.


PMP CSQA SCJP SCWCD SCBCD INS 21 INS 23
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q about instanceof
 
Similar Threads
Short circuit operators
instanceof Problem
Question from Ch 3 Self Test, Sierra/Bates Book
instanceof question
instanceof example not compiling