Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Cesar Gonzalez   on Mar 01 In Java Category.

  
Question Answered By: Luki Fischer   on Mar 01

I am trying to ignore exactly what it is your doing, but however offer
the following options.

If you are expecting an Integer object, then why not make the
parameter an integer?

public int  compareTo(Integer t){
int temp  = t.intValue();
return temp;
}

otherwise, check for the instance  so you won't get a
ClassCastException. Here's a couple of examples (I prefere the first
one):

public int compareTo(Object t){
int temp = DEFAULT_VALUE;
if (t instanceof Integer) {
temp = ((Integer)t).intValue();
}
return temp;
}

public int compareTo(Object t){
int temp = -1;

try {
temp = ((Integer)t).intValue();
} catch (Exception e) {
// ignore
temp = DEFAULT_VALUE;
}

return temp;
}

Share: 

 

This Question has 6 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on How Do I Cast Object To Int ? Or get search suggestion and latest updates.


Tagged: