سؤال

I'm preparing for the AP Computer Science Exam and I'm confused by my text's discussion of the Integer class.

The text defines Integer's compareTo as follows:

 int compareTo(Object other)

It made since to me at first that it would have an Object parameter because I thought that was the only way it could implement Comparable. However, I ended up looking at the Integer class in Eclipse and I noticed that it uses generics for Comparable and thus defines compareTo with an Integer parameter.

Is this a mistake in my book? It led me to believe that it was casting the Object to an Integer in the method, which is wrong (and makes a difference on the exam). Am I missing something here?

هل كانت مفيدة؟

المحلول

That was how the code looked in pre-Java 5 versions, when generics were not there. This also shows that the book you're referring is toooooo old. Please avoid.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top