Incompatibility between JDK 1.4 and JDK 1.6

Discussion in 'Java' started by Mike Schilling, Sep 26, 2009.

  1. 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.
     
    Mike Schilling, Sep 26, 2009
    #1
    1. Advertising

  2. On 09/26/2009 08:05 AM, Mike Schilling wrote:
    > 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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Sep 26, 2009
    #2
    1. Advertising

  3. Robert Klemme wrote:
    > On 09/26/2009 08:05 AM, Mike Schilling wrote:
    >> 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


    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.
     
    Mike Schilling, Sep 27, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Sung
    Replies:
    3
    Views:
    905
    visage
    Aug 6, 2003
  2. David Lees
    Replies:
    1
    Views:
    286
    David Lees
    Mar 7, 2004
  3. Ulf Meinhardt
    Replies:
    0
    Views:
    6,437
    Ulf Meinhardt
    Aug 10, 2006
  4. Mohamed Fysal
    Replies:
    6
    Views:
    386
    Cholo Lennon
    Nov 12, 2007
  5. Eric
    Replies:
    2
    Views:
    115
Loading...

Share This Page