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 match subclasses. For instance, of C
is a subclass of A
, then the following will be true:
C c = new C();
assert c instanceof A;
However, the following will be false:
C c = new C();
assert !c.getClass().equals(A.class)