5

I am stuck at 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 have tried using
int i = (Integer) object; int i = (int) object; intValue()
but still no luck. If I use int i = (Integer) object; it produce error:incompatible types. If I use int i = (int) object; it produce error: inconvertible types.

Please Help. Thank you in advance.

2
  • 2
    It is a bad practice to write yourself a wrapper around existing wrappers. java.lang.Integer works fine. If you need an integer implementation with more features, I would invite you to rethink your design and naming conventions before really writing yourself a new Integer class. Commented Nov 24, 2014 at 7:46
  • 2
    @EricTobias, while you are correct, I am not sure that the OP intentionally attempts create a wrapper, rather the issue is caused of misused generics. Commented Nov 24, 2014 at 8:15

2 Answers 2

10

You've replaced java.lang.Integer with a generic type which you have named Integer

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

should be

public boolean check(Integer num)
Sign up to request clarification or add additional context in comments.

Comments

3

After java 1.5 Autoboxing was supported. So you can use

int i = num;

1 Comment

You mention Autoboxing but your example is actually the reverse; Autounboxing. Also, with OP's generic this would not have worked because <Integer> was a generic type (and not java.lang.Integer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.