Search code examples
javatypeof

Figuring out whether a number is a Double in Java


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.


Solution

  • Try this:

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