54

Possible Duplicate:
How to determine an object's class (in Java)?
Java determine which class an object is

I have following sample incomplete method to compare the object type of a given object

public void test(Object value) {

        if (value.getClass() == Integer) {
            System.out.println("This is an Integer");
        }else if(value.getClass() == String){
            System.out.println("This is a String");
        }else if(value.getClass() == Float){
            System.out.println("This is a Float");
        }

}

The method can be called as:

test("Test");
test(12);
test(10.5f);

this method is not actually working, please help me to make it work

Rohan Dhar
  • 1,827
  • 1
  • 10
  • 21
Harsha
  • 3,548
  • 20
  • 52
  • 75

4 Answers4

78

You forgot the .class:

if (value.getClass() == Integer.class) {
    System.out.println("This is an Integer");
} 
else if (value.getClass() == String.class) {
    System.out.println("This is a String");
}
else if (value.getClass() == Float.class) {
    System.out.println("This is a Float");
}

Note that this kind of code is usually the sign of a poor OO design.

Also note that comparing the class of an object with a class and using instanceof is not the same thing. For example:

"foo".getClass() == Object.class

is false, whereas

"foo" instanceof Object

is true.

Whether one or the other must be used depends on your requirements.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • This is so painful. Currently we have to [implement Pattern Matching ourselves](https://kerflyn.wordpress.com/2012/05/09/towards-pattern-matching-in-java/) in every project. [Proposal to add to language.](http://openjdk.java.net/jeps/305) – JMess Aug 21 '18 at 19:48
30

You can compare class tokens to each other, so you could use value.getClass() == Integer.class. However, the simpler and more canonical way is to use instanceof :

    if (value instanceof Integer) {
        System.out.println("This is an Integer");
    } else if(value instanceof String) {
        System.out.println("This is a String");
    } else if(value instanceof Float) {
        System.out.println("This is a Float");
    }

Notes:

  • the only difference between the two is that comparing class tokens detects exact matches only, while instanceof C matches for subclasses of C too. However, in this case all the classes listed are final, so they have no subclasses. Thus instanceof is probably fine here.
  • as JB Nizet stated, such checks are not OO design. You may be able to solve this problem in a more OO way, e.g.

    System.out.println("This is a(n) " + value.getClass().getSimpleName());
    
Nick S.
  • 21
  • 5
Péter Török
  • 114,404
  • 31
  • 268
  • 329
7

You want instanceof:

if (value instanceof Integer)

This will be true even for subclasses, which is usually what you want, and it is also null-safe. If you really need the exact same class, you could do

if (value.getClass() == Integer.class)

or

if (Integer.class.equals(value.getClass())
Thilo
  • 257,207
  • 101
  • 511
  • 656
4

Use value instanceof YourClass

Konstantin V. Salikhov
  • 4,554
  • 2
  • 35
  • 48