Figuring out whether a number is a Double in Java

Kerzin picture Kerzin · Oct 29, 2009 · Viewed 187.9k times · Source

I'm a Java newbie. I'm trying to figure out whether a number is a Double with something like this:

if ( typeof ( items.elementAt(1) )== Double ) {
       sum.add( i, items.elementAt(1));
}

Would appreciate if someone could tell me how to rearrange the syntax to make this work properly.

Answer

ski picture ski · Oct 29, 2009

Try this:

if (items.elementAt(1) instanceof Double) {
   sum.add( i, items.elementAt(1));
}