Use variable to any type and check type of this in Java?

12,517

Solution 1

Use the instanceof operator.

For example:

if (o instanceof Integer) {
   //do something
} else if (o instanceof String) {
   //do something else
} else if (o instanceof Object[]) {
   //or do some other thing
} else if (o instanceof SomeCustomObject) {
   //....
}

Solution 2

The Java language provides an operator "instanceof" to check the runtime type of an object. You could check if an object is of any type, simple doing the following:

    if (o instanceof String) {
      // The Object is an instance of a String
    } 
    else if (o instanceof Double) {
      // The Object is an instance of a Double
    } 
    // And so on..

Another idea is to use the getClass, it works in a similar manner:

  if (o1.getClass().equals(o2.getClass())) {  
    // The objects have the same class type
  } 

Solution 3

There is no type in Java that is a supertype of both reference types and primitive types. But there is an alternative. Each primitive type has a corresponding immutable wrapper type; e.g.

  • boolean -> Boolean
  • int -> Integer
  • char -> Character

So you can wrap an int as an Integer and then assign the resulting value to a variable of type Object. (And in fact, modern versions of Java provide syntactic sugar that does the "boxing" and "unboxing" automatically; see http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Once you've assigned a value to a variable of type Object you can find out what its real type is using the instanceof operator ... or by using getClass().getName() to find out the type's name.

Solution 4

IN ADDITION JUST HAVE A LOOK AT

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#isPrimitive()

"There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double."

Share:
12,517
ephramd
Author by

ephramd

Updated on June 07, 2022

Comments

  • ephramd
    ephramd almost 2 years

    I have serious problems when implementing a java library for use in Android. I need global variables, I've solved this by applying Singleton.

    But now I need to use variables without specifying the type. As a solution I found using Object o.

    Object o, how I check the type of o?

    o.isArray () // Ok is type Array

    But to know if it is int, double, ...?

    Another solution to using Object or variable of any type?

    For example:

    public String arrayToString (Object o) {
        if (o.getClass (). isArray ()) {
            Arrays.toString return ((Object []) o);
            Else {}
            o.toString return ();
        }
    }
    
    Object [] a = (Object []) LIB.getConf ("test");
    a_edit [0] = "new value";
    a_edit [1] = 2013;
    
    x.arrayToString ("test") / / return test
    x.arrayToString (1989) / / return 1989
    x.arrayToString (a) / / return [new value, 2013]
    

    thanks you,