Java - Cannot convert Integer to int

I’ve been stuck in this problem for ages. Basically, I cannot convert Integer to int.

class CheckOdd implements Check<Integer>
{
  public <Integer> boolean check(Integer num)
  {
    int i = (Integer) num;
    return (i % 2 != 0);
  }
}

I tried using int i = (Integer) object; int i = (int) object; intValue()
but still no luck. If I use int i = (Integer) object;, it produces error:incompatible types. If I use int i = (int) object;, it produces error: inconvertible types.

Please, help. Thanks in advance.

+4
source share
2 answers

You replaced java.lang.Integerwith the generic type that you calledInteger

public <Integer> boolean check(Integer num) // <-- not a java.lang.Integer

it should be

public boolean check(Integer num)
+9
source

After java 1.5, Autoblock was supported. So you can use

int i = num;
+2
source

All Articles