Page 1 of 1

instanceof operator question

Posted: Tue Apr 30, 2013 9:15 pm
by berzerk
Hi,

I've just joined - currently studying for my OCJA - in three weeks time. Over the last week I've done the first two Enthuware mock exams - 70% and 71% - but in the second exam I had to go over the allotted time by a fair bit to finish.

Anyway - for $10, Enthuware is such good value and I've learnt heaps from reading the explanations and testing them. I want to nail the OCJA then go on to the OCJP

Here is a bit of code comparing (seemingly) incompatible objects with the instanceof operator.

My question - sure - I can understand why String and Date are incompatible; likewise, Random and Date, but why aren't HashMap and Date incompatible?

the compiler doesn't seem to mind this line:

Code: Select all

System.out.println(m instanceof Date);

Code: Select all

import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import java.util.Random;

public class Stuff {
   
	public static void main(String[] args) {
		Map m = new HashMap();
		String s = new String();
		Random r = new Random();
		
		System.out.println(m instanceof Date); //compatible - why ??
		System.out.println(s instanceof Date); //incompatible types
		System.out.println(r instanceof Date); //incompatible types
	}   
}

Re: instanceof operator question

Posted: Wed May 01, 2013 4:52 pm
by admin
Good question. This is because m is declared of type Map, which is an interface. Now, it is possible to have a class that extends Date and implement Map and so at run time it is possible for m to point to an object that implements Map and is still instanceof Date. So the compiler is happy :)

HTH,
Paul.

Re: instanceof operator question

Posted: Thu May 02, 2013 10:12 pm
by berzerk
Thanks Paul - that explanation makes sense. But writing another example using my own interface and classes - showing an instanceof operator successfully compiling yet still returning false had me still confused for a bit!

This is my example:

Code: Select all

interface Enjoyable {}

class Food {}
class Pie extends Food implements Enjoyable {}
class Bicycle implements Enjoyable {}

class General{
    public static void main(String[]args){
        Enjoyable myBike = new Bicycle();
        Enjoyable beefPie = new Pie();

        System.out.println(myBike instanceof Food); // compiles, prints false
        System.out.println(beefPie instanceof Food); //compiles, prints true
    }
}

Re: instanceof operator question

Posted: Fri May 03, 2013 6:33 am
by admin
Just because it compiles doesn't mean it will always return true. It will return true if the object pointed to by the reference at runtime is indeed an instance of the class on the right hand side of the instanceof operator. If you are not sure why your code prints false and true, ask yourself - what is the actual type of object (at runtime) on the left hand side? what is the class on the right hand side? Should it return true/false?

-Paul.

Re: instanceof operator question

Posted: Mon May 06, 2013 5:57 pm
by berzerk
What I found a little tricky was getting a compiling instanceof example to return false ie. coming up with the above example :D

(did test 3 yesterday, 72%)

Re: instanceof operator question

Posted: Thu Jul 13, 2017 12:16 am
by antoniosarco
Java instanceof is a keyword. It is a binary operator used to test if an object (instance) is a subtype of a given Type. It returns either true or false. More about...instanceof keyword

Anto