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


i have serious problems when implementing java library use in android. need global variables, i've solved applying singleton.

but i need use variables without specifying type. solution found using object o.

object o, how check type of o?

o.isarray () // ok type array

but know if int, double, ...?

another solution using object or variable of type?

for example:

public string arraytostring (object o) {     if (o.getclass (). isarray ()) {         arrays.tostring return ((object []) o);         else {}         o.tostring return ();     } }  object [] = (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,

use instanceof operator.

for example:

if (o instanceof integer) {    //do } else if (o instanceof string) {    //do else } else if (o instanceof object[]) {    //or other thing } else if (o instanceof somecustomobject) {    //.... } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -