Incompatibility between JDK 1.4 and JDK 1.6

M

Mike Schilling

Consider the following simple class:

class Compare
{
public int meth(String s, Object o)
{
return s.compareTo(o);
}
}

This compiles just fine under JDK 1.4, where Comparable contains the
signature compareTo(Object o); But in JDK 1.6, where Comparable<T>
contains compareTo(T o), compiling it gives

% c:/jdk1.6/bin/javac -g Compare.java
Compare.java:5: compareTo(java.lang.String) in java.lang.String cannot
be applied to (java.lang.Object)
return s.compareTo(o);
^
1 error

I can see why it works this way, but it still surprised me; I hadn't
thought that generics would cause existing code not to compile (as
opposed to generating tons of warnings, which I'm used to.)
Obviously, String (and the other system classes that now implement
Comparable<T>) could still contain compareTo(Object) for
compatibility.
 
R

Robert Klemme

Consider the following simple class:

class Compare
{
public int meth(String s, Object o)
{
return s.compareTo(o);
}
}

This compiles just fine under JDK 1.4, where Comparable contains the
signature compareTo(Object o); But in JDK 1.6, where Comparable<T>
contains compareTo(T o), compiling it gives

% c:/jdk1.6/bin/javac -g Compare.java
Compare.java:5: compareTo(java.lang.String) in java.lang.String cannot
be applied to (java.lang.Object)
return s.compareTo(o);
^
1 error

I can see why it works this way, but it still surprised me; I hadn't
thought that generics would cause existing code not to compile (as
opposed to generating tons of warnings, which I'm used to.)

You probably shouldn't. They do have to tell you something. :)
Obviously, String (and the other system classes that now implement
Comparable<T>) could still contain compareTo(Object) for
compatibility.

I don't think they can. IIRC method is defined in terms of Object, i.e.
compareTo(Object o) so you cannot have this method and the generic one
since type parameter T in Java 6 is not restricted and thus is erased to
Object:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html#compareTo(java.lang.Object)
http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html

Kind regards

robert
 
M

Mike Schilling

Robert said:
You probably shouldn't. They do have to tell you something. :)


I don't think they can. IIRC method is defined in terms of Object,
i.e. compareTo(Object o) so you cannot have this method and the
generic one since type parameter T in Java 6 is not restricted and
thus is erased to Object:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html#compareTo(java.lang.Object)
http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html

Looking into this a bit further, the change is that:

Iin 1.4, String implements Comparable, which means that it contains
compareTo(Object).

In 1.5, String implements Comparable<String>, which means that it contains
compareTo(String) from which the compiler generates a synthetic
compareTo(Object). The result is that code which calls
String.compareTo(Object) will continue to run, but will no longer compile.
I presume that's within Sun's goals for compatibility.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top