• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance Of operator

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When do instance of operator throw up compilattion error and when it evaluates to false?
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The instanceof operator is used to check if an object reference belongs to a certain type or not.
The type can either be an interface or a superclass.

The use of instanceof in java is as follows

Example
------- if(op1 instanceof op2) ....where op1 is a reference of any object & op1 is any class name.



During compile time the JVM will check whether the op1 is of type op2 or not.If it is not of same type then it will through compile time exception;
During runtime it will return true only if the Object which is referenced by op1 is not null & of same type
 
Ann Sebastian
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could any body give an example in which
1.instance of operator throws compilation error
2.instance of operator returns false
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The instanceof operator returns true when the first operand is not null and it can be casted to a type of the second operand without throwing a ClassCastException.

If the first operand is null and it can be casted to a type of the second operand without throwing a ClassCastException, instanceof returns false.

If the first operand cannot be casted to a type of the second operand without throwing a ClassCastException, there is a compile-time error.

Here it is an example,

class Point { int x, y; }
class Element { int atomicNumber; }
class Element2 extends Element{}
public class ClsPruebas1 {


public static void main(String[] args) {
Point p = new Point();
Element e = new Element();

Element2 e2=new Element2();
Element2 e3=null;

System.out.println(e2 instanceof Element); //returns true
System.out.println(e3 instanceof Element); //returns false
System.out.println(e3 instanceof Point); //compiler-time error
}

}

Hope it helps,

Cristina
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ann, a formal definition can be found here:JLS instanceof specification
HTH,
Sashi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic