Problem in instanceof operator

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
RRRRRR
Posts: 26
Joined: Sun Jul 23, 2017 2:13 am
Contact:

Problem in instanceof operator

Post by RRRRRR »

Hello I wanted to ask question on instanceof operator.

Suppose->

interface Face{ }
class Bar implements Face{ }
class Foo extends Bar{ }

NOW=>

Foo[] instanceof Foo, Bar, Face //false but how??

Also =>

Foo[1] is instanceof Foo,Bar,Face //true but how??

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Problem in instanceof operator

Post by admin »

Because an array is an instance of a special class defined by the compiler. For example, an array of type Foo is an instance of class named [Foo. Each element of this array is, of course, an instance of Foo (because you created an array of Foo). So foo[1] is an instance of Foo.
You can try this program to learn more:

Code: Select all

public lass TestClass{
  public static void main(String[] name){
    int[] ia = new int[10]; 
    boolean[] ba = new boolean[3]; 
    String[] stra = new String[5]; 
    TestClass[] ta = new TestClass[5];
    System.out.println(ia.getClass().getName());
    System.out.println(ba.getClass().getName());
    System.out.println(stra.getClass().getName());
    System.out.println(ta.getClass().getName());
  }
}
If you like our products and services, please help us by posting your review here.

RRRRRR
Posts: 26
Joined: Sun Jul 23, 2017 2:13 am
Contact:

Re: Problem in instanceof operator

Post by RRRRRR »

Sir see this->

interface Face{}
class Bar implements Face{}
class Foo extends Bar{}

1->class Collar{

2-> public static void main(String[] args)
3-> {
4-> Foo[]f={};
5-> System.out.println(f instanceof Foo); //Compil. error
6-> System.out.println(f instanceof Bar); //Compilation Error
7-> System.out.println(f instanceof Face); //CT error
8-> System.out.println(f instanceof Object); //Compiles succesfully
}

9-> }


I am can't be able to know that why at line 5,6,7 there is coming CT error since I am trying to check whether f is instance of Foo class or instance of Bar class or Face Interface.

Then how I check that whether the array variable is instance of class Foo class Bar and interafce Face??

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Problem in instanceof operator

Post by admin »

Since f is declared to be of type array of Foo, the compiler knows that f can never point to an instance of class Foo. That is why it refuses to compile f instanceof Foo and f instance of Bar etc.

f instanceof Object compiles because no matter what kind of object f points to, it will always be an instance of Object, so it compiles fine.

There is no need to check whether f is an instance of Foo because you already know that it can never be an instance of Foo. Compiler knows that as well. f instanceof Foo is a non-sensical check, that is why the compiler does not accept it.
If you like our products and services, please help us by posting your review here.

RRRRRR
Posts: 26
Joined: Sun Jul 23, 2017 2:13 am
Contact:

Re: Problem in instanceof operator

Post by RRRRRR »

Sir I can't be able to understand this please tell how it is Compiling fine->

1) First Operand 2) instanceof Operand 3) Result
(reference being tested) (type we are comparing
the reference against)


1.a)-> Foo[] 2.a) Foo,Bar,Face 3.a) False

2.a)-> Foo[1] 2.b) Foo,Bar,Face,Object 3.b) True
Last edited by RRRRRR on Fri Aug 04, 2017 9:36 am, edited 1 time in total.

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Problem in instanceof operator

Post by admin »

Did you even read my responses above??? It explains exactly what you are asking.
Read them again. Slowly. And run the program that I showed above.
If you like our products and services, please help us by posting your review here.

RRRRRR
Posts: 26
Joined: Sun Jul 23, 2017 2:13 am
Contact:

Re: Problem in instanceof operator

Post by RRRRRR »

Sir i can't understand that ->

Once we are testing Foo[] with Bar,Foo,Face and it is getting false
then we are testing Foo[1]with Bar,Foo,Face and it is getting true
But how??

since to test we write this->

Foo f[]={};

(f instanceof Foo[]) //compiles fine

and if we write->

Foo f[]=null;
(f instanceof Foo[])//compiles fine

so in both cases we need to do comparison b/w ref. variable and an array?? // Am I right?


because if we don't put array on RHS of instanceof we are getting incompatible types.

RRRRRR
Posts: 26
Joined: Sun Jul 23, 2017 2:13 am
Contact:

Re: Problem in instanceof operator

Post by RRRRRR »

Fine sir I got the point now :thumbup:

Thank you for your support and sorry for Teasing you so much :P

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Problem in instanceof operator

Post by admin »

Compiler does not check if the variable points to an actual object of that type. If only checks if it is possible for the variable to point to an object of the type that you are checking. That is why:
Foo f[]=null;
(f instanceof Foo[]) compiles fine

It will return false at runtime because f does not point to an object of type Foo[].

f instanceof Foo does not compile because the compiler knows that f can never point to an instance of type Foo.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: admin, Bing [Bot] and 58 guests