aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes once again instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "once again instanceof" Watch "once again instanceof" New topic
Author

once again instanceof

sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

See the foolowing code:


Even though b is array of class B
It gives compiler error as at line 1
inconvertible types.
found : B{]
required :B

while line2 compiles and runs

I am not getting why it happens .


Thanks & Regards
Sumit Kothalikar
sachin burange
Ranch Hand

Joined: Jun 24, 2008
Posts: 41
b is a list of objects where as B is a class that's why it is giving error.

Regards
Sachin
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 62

Slightly Modified instance class to understand little more..

Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

sumit kothalikar wrote:See the foolowing code:


Even though b is array of class B
It gives compiler error as at line 1
inconvertible types.
found : B{]
required :B

while line2 compiles and runs

I am not getting why it happens .


b is an array and it cannot be instance of class B
here we are not initializing array elements
hence they are pointing to null
as null is not instance of anything
hence b[0] instanceof B[] returns false
and b[0] instanceof Object returns true
if we write
b[0] = new B();
then
b[0] instanceof B returns true
b[0] instanceof Object returns true

hope this is clear to you
Happy coding

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Hi prasad
will you explain with a code how to use array instance of class in instanceof operator
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438


at line 1 you get error inconvertible types it is explained in the comments above
when you remove or comment line 1 and then run the program
we get the output as follows
C:\Users\Prasad\Desktop>java A
null instanceof Object false
a[0] instanceof A false
a[1] instanceof A true
Done

Hope this is clear to you now
still if you have got problems feel free to ask
happy coding
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

I got it so we can't compare instanceof class arrays with there classes.
Instead we have to intialised them to some class objects
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: once again instanceof