File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof Object []

 
Juliet Meguma
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

In my Java book, I just read that the following line is not legal:

if (x instanceof Object[] )

The reason given is that "the array (x) may be of a primitive type, in which case the test will fail". And that's the extent of the explanation - which left me confused.

So I wrote a little test for myself, where x is an int array. When i tried to compile code with that illegal line in it I got a compiler error "Inconvertible types".

Does the compiler test that
1) both arguments are arrays (test passes)
2) element types of both arrays are the same - but it uses arithmetic promotion to promote the int type up to an Object type before doing so (which results in the "Inconvertible types" error)?

Am I on the right track or waaaay off..?

Any help would be really appreciated
Thanks!
Juliet
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea is that if the compiler can prove that no object that could be referred to by the left operand could ever be referred to by the right operand, it's a compile time error.

See this thread:
saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=025023
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic