Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Incompatibility between JDK 1.4 and JDK 1.6

Reply
Thread Tools

Incompatibility between JDK 1.4 and JDK 1.6

 
 
Mike Schilling
Guest
Posts: n/a
 
      09-26-2009
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.


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-26-2009
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/...va.lang.Object)
http://java.sun.com/javase/6/docs/ap...omparable.html

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      09-27-2009
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/...va.lang.Object)
> http://java.sun.com/javase/6/docs/ap...omparable.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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
structure incompatibility between VB and VC++ Mohamed Fysal C++ 6 11-12-2007 02:33 AM
What is the difference between J2EE, JDK, JDK-SDK, JRE and J2SE packages ? Ulf Meinhardt Java 0 08-10-2006 07:12 PM
IIS Versions incompatibility between Server and Client Eric ASP .Net Security 2 07-01-2005 12:12 PM
Incompatibility between psyco 1.2 and pythonwin? David Lees Python 1 03-07-2004 07:09 AM
Re: float incompatibility between Windows and UNIX? Alan Sung C++ 3 08-06-2003 12:31 PM



Advertisments