Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

instanceof operator  RSS feed

 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody tell me how instanceof operator works?
Code:
-------------------------------------------------
import java.util.*;

public class Class43 {
public static void main(String args[]) {
Object a1 = new LinkedList(), b1 = new TreeSet();
Object c1 = new TreeMap();
System.out.print((a1 instanceof Collection) + ",");
System.out.print((b1 instanceof Collection) + ",");
System.out.print(c1 instanceof Collection);
}
}
------------------------------------------------------------------
Query: In the above code, the output is true,true,false. My question is
when LinkedList, TreeSet are upcasted to assigned to an Object type reference variable then how instanceof operator determines its actual nature.

Thanks,
Ritu
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List and Set both extend Collection,

instanceof returns true if object on left can be assigned to Right hand side,

as LinkedList implements List so it can be assigned to Collection
same with TreeSet it implements Set.

Map does not extends Collection so it can not be assigned to Collection type hence false.
[ October 06, 2005: Message edited by: Santana Iyer ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You are checking whether the objects...created by them....not the reference ..type... are instances of Collection...

Here LinkedList,TreeSet is an instance of Collection ...so true..

because they indirectly implement..Collection interface (List ,Set extends Collection)

TreeMap doesnt ..so false..

Hope you got it..


[ October 06, 2005: Message edited by: A Kumar ]
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still have doubt. Please elaborate a bit.

In the above code whether the instanceof operator compares with the type of reference variable(i.e Object type) or the object(i.e Collection type) to which the reference variable is pointing.

And when does this instanceof operator works-
1. during compilation time
2. or during runtime

Thanks
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it works for all IS-A relationship. If A IS-A B then, a's objects are instanceof A as well as B.

A extends B------> A IS-A B
A implements B -----------> A IS-A B

All objects of A will indicate a green signal for being instance of A and being instance of B.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



And when does this instanceof operator works-
1. during compilation time
2. or during runtime



Run Time..



 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now the question is if instanceof operator compares the left side object(not the reference variable) to the right hand side class at runtime the what are cases where type of reference variables plays part in determing the

What I've concluded from the above responses that:
1. type cast is resolved at runtime
2. Type of reference variable does not play part in resolving the type of object while the actual object at the L.H.S is used in instanceof operator.

If the above statements are true, then what is actually happening at //1.

Object obj=new LinkedList();
if(obj instanceof Collection) //1
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All is explained Here

What happens in 1 is : obj holds a LinkedList, obj is not null, LinkedList implements Collection, we can cast obj to Collection without errors, so obj instanceof Collection returns true.
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seb!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!