views:

388

answers:

3

Here's what I've been trying to do, in a nutshell:

class example <T extends Number>
{
    private int function(T number)
    {
        int x = (int) number;
        ...
    }
    ...
}

Basically, I'm trying to make it so that T is a number so I can convert it to an int inside that function. The problem is that I'm getting an "incovertible types" error, so I must be doing something wrong.

A: 

Use

x=number.intValue();
Software Monkey
+3  A: 

Considering that the abstract class Number is the superclass of classes * BigDecimal, BigInteger, * Byte, Double, Float, * Integer, Long, and Short...

if you want to have an int representation, you should use the .intValue() function.

If that is what you want to do.

VonC
+1  A: 

For your example, I don't think you need generics. I think you can just:

private int func(Number n) {
  return n.intValue();
}

If you're trying to do something with generics, I'd take that out at the moment and get just the above working, first.

skiphoppy