• Post Reply Bookmark Topic Watch Topic
  • New Topic

error due to compareTo method java 1.5  RSS feed

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are upgrading to jdk 1.5. I am getting compilation errors due to compareTo method which were working in jdk 1.4

Here is what I get

method compareTo(Integer) in the type Integer is not applicable to compareTo(Object).

I know I can simply cast the object to Integer but is it possible without casting or is it a change that came wioth 1.5

I ask this because to change one line I'll have to go through a new service request which would need to be approved before I can change this line and of course I would have to document it to.

Thanks
Imad
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java 5, the Comparable interface has been granted the power of generics. This means that when a class is created that implements Comparable, it can specify what it can compare itself to. This doesn't need to be just Object, but can be any subclass. Mostly they can only compare themselves to objects of the same class or a subclass:

As you can see, compareTo doesn't accept Object anymore - it accepts MyClass, or whatever class is used.

Integer, String, Long etc now all can only compare themselves to their own classes, since they implements Comparable<Integer>, Comparable<String> and Comparable<Long> respectively.
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply...i guess We'd have to change this three lines of code.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that Java 5 is upwards compatible with Java 1.4 - the old Java 1.4 method without generics still works, you only might get a warning (not an error) from the compiler about using raw types:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!