• Post Reply Bookmark Topic Watch Topic
  • New Topic

newbie in trouble about instanceof  RSS feed

 
Tianpeng Xia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi, everyone, above is my code,the output is expected to be "N", but i just got an error,something like "require:Integer","found:String".
could anyone explain this? thanks!
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Also please UseCodeTags to post your code
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. When posting code, please UseCodeTags(⇐click) so it will be readable.

2. Don't describe an error as "something like..." Instead, copy/paste the exact, complete error message, and indicate clearly exactly which line is causing it.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Also please UseCodeTags to post your code



thanks, i'll try to use that.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:1. When posting code, please UseCodeTags(⇐click) so it will be readable.

2. Don't describe an error as "something like..." Instead, copy/paste the exact, complete error message, and indicate clearly exactly which line is causing it.

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.....
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the base language on your PC to English, copy the error message, and change the language back.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!