How does the instanceof operator work in Java?
-
Under the covers, in as much detail as you know.
-
Answer:
The instanceof operator, like the rest of Java, works at both the compiler level and at the virtual machine level. At compile time, the instanceof is checked and compiled into byte code. The checks are detailed in the Java Language Specification: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2 The left side must be a reference or null, and not a primitive type. The right side must be a non-generic type, even if it is a generic class. For example, java.util.List<T> is not allowed. This shows that generic types are erased at runtime. The compile-time type of the left side must allow casting to the right side. instanceof should only be used for downcasting, in most cases. At runtime, the check is detailed in the Java Virtual Machine Specification: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.instanceof To quote, with the statement being S instanceof T, the result is true if and only if: If S is an ordinary (nonarray) class, then: If T is a class type, then S must be the same class as T, or S must be a subclass of T; If T is an interface type, then S must implement interface T. If S is an interface type, then: If T is a class type, then T must be Object. If T is an interface type, then T must be the same interface as S or a superinterface of S. If S is a class representing the array type SC[], that is, an array of components of type SC, then: If T is a class type, then T must be Object. If T is an interface type, then T must be one of the interfaces implemented by arrays (JLS §4.10.3). If T is an array type TC[], that is, an array of components of type TC, then one of the following must be true: TC and SC are the same primitive type. TC and SC are reference types, and type SC can be cast to TC by these run-time rules
Miguel Paraz at Quora Visit the source
Other answers
1. Every object created out of a class is an object.(i assume you know what I mean here) 2. Any class thus created in a java program is an object in itself. 3. But, when I use instanceof, it checks the immediate hierarchy or the next level hierarchy to check whether the object I am trying to know is really a "type of" that class. A very good example is here - http://www.coderanch.com/t/492221/java-programmer-SCJP/certification/instanceof-operator-works
Kranthi Paidi
Instanceof operator: (comparison operator) Instanceof operator is used to check whether object is an instance of specified object. It returns Boolean value i.e. true or false. [code] class Demo{ public static void main(String args[]){ Demo s=new Demo(); System.out.println(s instanceof Demo);//true } } [/code] OUTPUT: true For more Details Refer following link: http://www.javatpoint.com/downcasting-with-instanceof-operator
Manisha Mulchandani
Related Q & A:
- How to connect to bluetooth device in Java?Best solution by Stack Overflow
- How do I make a window in Java?Best solution by Stack Overflow
- How to start programming a game in java?Best solution by Game Development
- How to get output on screen with java?Best solution by Stack Overflow
- How do I retrieve a URL in Java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.