aspose file tools*
The moose likes Java in General and the fly likes instanceof Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "instanceof Operator" Watch "instanceof Operator" New topic
Author

instanceof Operator

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

Hi ,
In this below code the complier error that is coming



Instance_Of_Test.java:7: inconvertible types
found : Bar[]
required: Bar
if(b instanceof Bar)
^
1 error

My ques is why this is so that array object is not passing the instanceof for Bar class but passing for the Object class.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
upanshu vaid wrote:My ques is why this is so that array object is not passing the instanceof for Bar class but passing for the Object class.

Because the type of b is array of Bar and an array of Bar can never be an instance of Bar so the compiler does not allow it.
All arrays in Java are also Objects so b is an instance of Object.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

upanshu vaid wrote:In this below code the complier error that is coming

If that is exactly what you got, then I see your problem, and I sympathise. Classic case of a badly created compiler message.
I suspect if it had said:it would have been a lot clearer.

Unfortunately, compiler writers aren't paid for their language skills; and what seems obvious to them is torture to puny humans like us.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof Operator