Sagar Vaghela
Sagar Vaghela

Reputation: 1273

Get all Integer data type fields using reflection

I want to find the data type of field using its name. I get Type from \

Class<?> type = f.getType()

but can't determine which is Integer or String or other. How compare type is Integer or String or Other.

Upvotes: 1

Views: 525

Answers (2)

Jens
Jens

Reputation: 69505

Try this:

       if (f.getType().getName().equals(String.class.getName())) {
           System.out.println("String");
       }
       if (f.getType().getName().equals(Integer.class.getName())) {
           System.out.println("Integer");
       }

Upvotes: 0

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

You can use the <class>.class structure and compare the type. You can get the type with the getClass method.

Class<?> type = f.getClass();
if (type == Integer.class) {
    // integer 
} else if (type == String.class) {
    // string
} else {
    // other
} 

Upvotes: 2

Related Questions