// Purpose. instanceof operator versus reflection [getClass(), getName()] class ReflectInstanceOf { static class Base { } static class Derived extends Base { } static class Deep extends Derived { } static class Alone { } public static void main( String[] args ) { // "instanceof" returns true if the object is an instance of the // specifed class -- or -- an instance of some derived class Object[] objs = { new Base(), new Derived(), new Deep(), new Alone() }; for (int i=0; i < objs.length; i++) if (objs[i] instanceof Base) System.out.println( objs[i].getClass().getName() + " IS an instanceof" ); else System.out.println( objs[i].getClass().getName() + " is NOT an instanceof" ); // the equality expression below returns true only if the object is // an instance of the specifed class [Note the ".class" suffix] System.out.println(); for (int i=0; i < objs.length; i++) if (objs[i].getClass() == Base.class) System.out.println( objs[i].getClass().getName() +" IS a Base"); else System.out.println( objs[i].getClass().getName() +" is NOT a Base"); } } // Base IS an instanceof // Derived IS an instanceof // Deep IS an instanceof // Alone is NOT an instanceof // // Base IS a Base // Derived is NOT a Base // Deep is NOT a Base // Alone is NOT a Base