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

doubt in instanceof

aleyx chow
Greenhorn

Joined: May 15, 2005
Posts: 16
Why the following code occur a compile err?



i think the ouput is false,but...JVM told me:
inconvertile types:
Found: String[]
Required: String

in K&B book 3.1.3 there's a sample,but it cant give a clue to my question,i think.the sample is following:

in table 3.2
Foo[] instanceof Foo/Bar/Face the result is false,
i try it,but catch an exception, can somebody give me a finally answer?
thanx.


Oh,father,though im in the nutshell,i still the king of limitless space.<br /> <br />scjp 1.4,scwcd 1.4,scbcd 1.3
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
instanceof operator can be used as Object instanceof Object. in the example u r indirectly trying it is Object[] instanceof Object. Hence the compiler error.


Thnx
aleyx chow
Greenhorn

Joined: May 15, 2005
Posts: 16
hi,Nischal,thanx for ur reply.
but...im still confused

why it still occur err?
code such as
System.out.println(new int[]{1} instanceof byte[])
got compile err but not print on the console "false"
why?
thanx.
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
Hi
Also, 1 more thing needs to be added in my earlier reply.

instanceof is a very smart operator. It solves misuse of instanceof operator i.e., dosent allow comparison of non related Objects. As u can see, Foo and Other have no relation at all, only they have the same parent but that still means no relation !!.. So, at compile time the compiler evaluates the statement foo instanceof Other and hence compiler error... However ur earlier post had a compiler error because of Object[] and Object comparison which is faulty ( we cant compare {"1","2"} with "1".....Hope this helps...
aleyx chow
Greenhorn

Joined: May 15, 2005
Posts: 16
thanx a lot,i catch ur meaning. :-)
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by aleyx chow:
[QB]hi,Nischal,thanx for ur reply.
but...im still confused



i didn't get the point that why foo instanceof Other is getting compiler error ...atleast it must return false ...
so in which case it will return false ?? if this is not the case

and also why bar0 instanceof Other is returning false when it should return compilor error as per above result..
as bar0 is also reference of superclass as foo ??

then why compiler error for one and not for other ?

and i dont know the reason for this

System.out.println(bar1 instanceof Other); //false
Bar bar2=new Other();
System.out.println(bar2 instanceof Other); //true

why this

seriously confused...


Thanks and Regards, Amit Taneja
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

When using instanceof, the declared type of the variable (which is not necessarily the runtime type of the instance) must be cast compatible with the class.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
pls explain this

Bar bar1=new Foo();
System.out.println(bar1 instanceof Other); //false
Bar bar2=new Other();
System.out.println(bar2 instanceof Other); //true

with reference to mark reply ?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
When you have a reference to a Foo Object the compiler knows the Object being referenced is of type Foo or a subclass of type Foo. The compiler is smart enough to know that it is impossible for an Object of type Foo, or a subclass of type Foo, to be and 'instanceof' an Object of type Other.

With Bar being the parent class, and Foo and Other being the subclasses it is an impossiblity for a Foo to be and Other.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by marc weber:
When using instanceof, the declared type of the variable (which is not necessarily the runtime type of the instance) must be cast compatible with the class.


hi steven .thanx for reply

but can u correlate with what marc said...and

i will thanxfull if u explain with what marc said...
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
To aleyx chow and amit

Referring back to your initial question, regarding the confusion with the following code giving compile-time error ,



I did not understand Nishal's reply

instanceof operator can be used as Object instanceof Object. in the example u r indirectly trying it is Object[] instanceof Object. Hence the compiler error.


I was not clear as to what he meant by "Object[]"

According to me you are trying to compare an object of type Array to an object of type String therefore the compiler is throwing an error message.

amit you had a problem with the following code

Bar bar1=new Foo();
System.out.println(bar1 instanceof Other); //false
Bar bar2=new Other();
System.out.println(bar2 instanceof Other); //true


Foo() and Other() are two completely different objects although they extend from the same object in this case it is Bar.

For example a Truck and a Car extend from Vehicle or Vehicle is a super class for Truck and Car, that does not mean that Car and Truck are the same objects.

Lets transform the above into a Car and Truck example to understand better.



As you can see bar1 is clearly not an instance of Truck even though it belongs to the class Vehicle.

Try the code below for Car, Truck example,

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by amit taneja:
...i will thanxfull if u explain with what marc said...

Consider the hierarchy...

An instance of Foo can be upcast to Bar, and such an instance can be downcast from Bar back to Foo. So Foo and Bar are cast compatible types. In the same manner, Other and Bar are cast compatible types. However, Foo and Other are not cast compatible because there is no upcast/downcast relationship.

Now, consider the examples...

Foo foo = new Foo();
The declared type of the variable is Foo, which is not cast compatible with Other. Therefore, foo instanceof Other results in a compilation error.

Bar bar0 = new Bar();
The declared type of the variable is Bar, which is cast compatible with Other, so the instanceof compiles. However, the runtime type of the instance is Bar, and a Bar is not an Other, so bar0 instanceof Other returns false.

Bar bar1 = new Foo();
The declared type of the variable is Bar, which is cast compatible with Other, so the instanceof compiles. However, the runtime type of the instance is Foo, and a Foo is not an Other, so bar1 instanceof Other returns false.

Bar bar2 = new Other();
The declared type of the variable is Bar, which is cast compatible with Other, so the instanceof compiles. In this case, the runtime type of the instance is Other, so bar2 instanceof Other returns true.
Shweta R Dhaneshwar
Ranch Hand

Joined: May 27, 2005
Posts: 35
Hi,
s is a reference variable which references array of string objects.If you see the class hierarchy ,it looks like String[] and String are child classes extending from Object class.Take a look at 'Completing the Type Hierarchy' of Khalid Mughal,page no.258...you will get a clear picture.I hope you find this explanation convincing.


Shweta Dhaneshwar.<br />SCJP 1.4 90%
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
marc ...fabulus answer..
thanx
now got it..

so final conclusion is that

reference type variable must in hirarchy with instanceof other_operand so it will atleast compile.
else compiler error
and if complied we will see the declared type of object which is refered to reference variable for true/false

thanx Abdulla Mamuwala u too...


regards,
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
just give final acknoledgement regarding my conclusion in the last post above...
 
jQuery in Action, 2nd edition
 
subject: doubt in instanceof