You can use the getClass() method, or you can use instanceof. for example
for (Object obj : list) { if (obj instanceof String) { ... } }
or
for (Object obj : list) { if (obj.getClass().equals(String.class)) { ... } }
Note that instanceof will correspond to subclasses. For example, from C is a subclass of A , then the following will be done:
C c = new C(); assert c instanceof A;
However, the following will be false:
C c = new C(); assert !c.getClass().equals(A.class)
faran Sep 19 '08 at 23:28 2008-09-19 23:28
source share