operators - Use of "instanceof" in Java

ID : 10019

viewed : 39

Tags : javaoperatorsinstanceofjava

Top 5 Answer for operators - Use of "instanceof" in Java

vote vote

93

Basically, you check if an object is an instance of a specific class. You normally use it, when you have a reference or parameter to an object that is of a super class or interface type and need to know whether the actual object has some other type (normally more concrete).

Example:

public void doSomething(Number param) {   if( param instanceof Double) {     System.out.println("param is a Double");   }   else if( param instanceof Integer) {     System.out.println("param is an Integer");   }    if( param instanceof Comparable) {     //subclasses of Number like Double etc. implement Comparable     //other subclasses might not -> you could pass Number instances that don't implement that interface     System.out.println("param is comparable");    } } 

Note that if you have to use that operator very often it is generally a hint that your design has some flaws. So in a well designed application you should have to use that operator as little as possible (of course there are exceptions to that general rule).

vote vote

81

instanceof is used to check if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

Read more from the Oracle language definition here.

vote vote

77

instanceof can be used to determine the actual type of an object:

class A { }   class C extends A { }  class D extends A { }   public static void testInstance(){     A c = new C();     A d = new D();     Assert.assertTrue(c instanceof A && d instanceof A);     Assert.assertTrue(c instanceof C && d instanceof D);     Assert.assertFalse(c instanceof D);     Assert.assertFalse(d instanceof C); } 
vote vote

64

instanceof is a keyword that can be used to test if an object is of a specified type.

Example :

public class MainClass {     public static void main(String[] a) {      String s = "Hello";     int i = 0;     String g;     if (s instanceof java.lang.String) {        // This is going to be printed        System.out.println("s is a String");     }     if (i instanceof Integer) {        // This is going to be printed as autoboxing will happen (int -> Integer)        System.out.println("i is an Integer");     }     if (g instanceof java.lang.String) {        // This case is not going to happen because g is not initialized and        // therefore is null and instanceof returns false for null.         System.out.println("g is a String");     }  }  

Here is my source.

vote vote

51

why is it that even managed languages provide a finally-block despite resources being deallocated automatically by the garbage collector anyway?

Actually, languages based on Garbage collectors need "finally" more. A garbage collector does not destroy your objects in a timely manner, so it can not be relied upon to clean up non-memory related issues correctly.

In terms of dynamically-allocated data, many would argue that you should be using smart-pointers.

However...

RAII moves the responsibility of exception safety from the user of the object to the designer

Sadly this is its own downfall. Old C programming habits die hard. When you're using a library written in C or a very C style, RAII won't have been used. Short of re-writing the entire API front-end, that's just what you have to work with. Then the lack of "finally" really bites.

Top 3 video Explaining operators - Use of "instanceof" in Java

Related QUESTION?