In this particular case, I am going to guess that it's the instanceof test that's causing the problem. The reason is that if you're going to test for instanceof, it has to be something that could possibly be the type you're testing for. A String reference cannot possibly point to an Integer object, so it's an error.

However, if you change the aString declaration line to this:

then the aString variable is a reference to Object, and since the Object it points to could possibly be an Integer, the instanceof test is valid.

Tianpeng Xia

Greenhorn

Posts: 16

posted 6 years ago

Mohamed Sanaulla wrote:If you read about the instanceof operator here, it states:

You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface

and none of these conditions are satisfied in the comparison of String and Integer instance, they both dont belong to the same hierarchy.

Mohamed Sanaulla wrote:If you read about the instanceof operator here, it states:

You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface

and none of these conditions are satisfied in the comparison of String and Integer instance,

The problem isn't that those conditions are not satisfied. It's fine if they're not satisfied. That's why the test exists--to find out if they are or are not. The problem is that they cannot possibly be satisfied because, as you point out...

they both dont belong to the same hierarchy.

Tianpeng Xia

Greenhorn

Posts: 16

posted 6 years ago

Jeff Verdegan wrote:1. When posting code, please UseCodeTags(⇐click) so it will be readable.

In this particular case, I am going to guess that it's the instanceof test that's causing the problem. The reason is that if you're going to test for instanceof, it has to be something that could possibly be the type you're testing for. A String reference cannot possibly point to an Integer object, so it's an error.

However, if you change the aString declaration line to this:

then the aString variable is a reference to Object, and since the Object it points to could possibly be an Integer, the instanceof test is valid.

thanks, Jeff. there is always a reluctance to post exact error message here because the message on my pc is not in English and i don't know how to precisely translate them.....