Hi, I am hoping someone can help explain the in's and out's of using instanceof.
Given the following code:
interface alpha{}
class beta implements alpha{}
class gamma extends beta{}
public class c13
{
public static void main(
String args[])
{
gamma []GA = new gamma[7];
GA[0] = new gamma();
System.out.println(GA instanceof gamma) // 1
System.out.println(GA instanceof beta ); // 2
System.out.println(GA instanceof alpha ); // 3
System.out.println(GA instanceof Object ); // 4
System.out.println(GA instanceof gamma[]); // 5
System.out.println(GA[0] instanceof gamma);// 6
System.out.println(GA[0] instanceof beta );// 7
System.out.println(GA[0] instanceof alpha);// 8
System.out.println(GA[0] instanceof Object);// 9
}
} // close class
When compiling I would expect the whole code to compile and give the results:
false//1
false//2
false//3
true//4
true//5
true//6
true//7
true//8
true//9
However whilst compiling I get the following error:
inconvertible types
found : gamma[]
required: gamma
System.out.println(GA instanceof gamma);
inconvertible types
found : gamma[]
required: gamma
System.out.println(GA instanceof beta);
inconvertible types
found : gamma[]
required: gamma
System.out.println(GA instanceof alpha);
This relates to the first three lines of the instanceof comparison.
When I comment out the 3 offending lines - the code then runs. But I can't understand why the first three lines will not compile! Any ideas why this is happening?