instanceof operator question

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

Moderator: admin

Post Reply
berzerk
Posts: 4
Joined: Tue Apr 30, 2013 8:48 pm
Contact:

instanceof operator question

Post 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
	}   
}

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

Re: instanceof operator question

Post 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.
If you like our products and services, please help us by posting your review here.

berzerk
Posts: 4
Joined: Tue Apr 30, 2013 8:48 pm
Contact:

Re: instanceof operator question

Post 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
    }
}

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

Re: instanceof operator question

Post 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.
If you like our products and services, please help us by posting your review here.

berzerk
Posts: 4
Joined: Tue Apr 30, 2013 8:48 pm
Contact:

Re: instanceof operator question

Post 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%)

antoniosarco
Posts: 2
Joined: Mon Feb 13, 2017 6:20 am
Contact:

Re: instanceof operator question

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests