instanceof Vs getClass( )
<p>I see gain in performance when using getClass () and == operator over instanceOf operator.</p>

<pre><code>    Object  str = new Integer("2000");

    long starttime = System.nanoTime();
    if (str instanceof String) {
        System.out.println("its string");
    }else {
        if (str instanceof Integer) {
            System.out.println("its integer");

        }
    }
    System.out.println((System.nanoTime()-starttime));

    starttime = System.nanoTime();
    if(str.getClass() == String.class){
        System.out.println("its string in equals");
    }else{
        if(str.getClass() == Integer.class){
            System.out.println("its integer");
        }
    }
    System.out.println((System.nanoTime()-starttime));
</code></pre>

<p>Is there any guideline, which one to use (getClass ( ) and instanceOf)?
Please post your views.</p>

<p>Edit:</p>

<p>Given a scenario , I know exact classes to be matched , that is String, Integer (these are final classes)etc.
Is using instanceOf operator bad practise ?</p>
