• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in instanceof

 
aleyx chow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
aleyx chow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx a lot,i catch ur meaning. :-)
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just give final acknoledgement regarding my conclusion in the last post above...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic