Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

unchecked call to compareTo(T)

Thread view: 
Mark - 02 Nov 2006 02:22 GMT
hi,

I was just wondering how I might get rid of this error.. can't seem to
figure it out.

the error:

warning: [unchecked] unchecked call to compareTo(T) as a member of the
raw type java.lang.Comparable
        else if( o.compareTo(n.o) < 0 )
                                   ^

the function:

    private void insert(Comparable o, Node n)
    {
        if( n == null )
            n = new Node(o);
        else if( o.compareTo(n.o) < 0 )
            insert(o,n.l);
        else
            insert(o,n.r);
    }

my node class:

class Node
{
    public Object    o = null;
    public Node        l = null;
    public Node        r = null;
   
    Node(Object obj) {
        o = obj;
    }
}
   

thanks guys.
Mark - 02 Nov 2006 08:15 GMT
> hi,
>
[quoted text clipped - 34 lines]
>
> thanks guys.

i'm serious.. I need help.

I cant do something like Comparable<Object> because then it only takes
objects..whereas I want I want it to accept anything that implements
Comparable.
Piotr Kobzda - 02 Nov 2006 10:14 GMT
[...]

> I cant do something like Comparable<Object> because then it only takes
> objects..whereas I want I want it to accept anything that implements
> Comparable.

How would accepting anything that implements Comparable be useful for you?

Having for example:

    Comparable c1 = "";
    Comparable c2 = 1;

both c1.compareTo(c2) and c2.compareTo(c1) will end in runtime exception.

You just can not simply compare things which are not comparable to each
other, even when they are all comparable to something (usually to
compatible type objects only).

The solution would be (and I suppose it's the best approach in your
case) applying more knowledge on data type held by Nodes.  You can
achieve that by parameterizing both your code and Node class and than
use Node<T> and Comparable<T> (or somewhat like Comparable<? super T>)
instead.

You can also solve it using Comparator<Object> which will be able to
compare two distinct type objects applying your specific comparison
rules in compare() method.  Unfortunately, this will most probably end
in _very awful_, not OO, implementation of compare(), similar to the
following one:

    public int compare(Object o1, Object o2) {
        if (o1 instanceof String)
            return ((String)o1).compareTo((String)o2);
        if (o1 instanceof Integer)
            return ((Integer)o1).compareTo((Integer)o2);
        ...

So I presume you'll need to rethink a little your design again.

piotr
Mark - 02 Nov 2006 10:51 GMT
> [...]
>
[quoted text clipped - 37 lines]
>
> piotr

Well, I guess I should have said that the objects can be of any type,
as long as they are of the same type.  Which I guess means I need to
make the Node generic as well... and thus my entire BinaryTree class
generic.  I haven't really dealt with generics in Java yet..so, it's
all new to me.
Mark - 03 Nov 2006 06:25 GMT
> The solution would be (and I suppose it's the best approach in your
> case) applying more knowledge on data type held by Nodes.  You can
> achieve that by parameterizing both your code and Node class and than
> use Node<T> and Comparable<T> (or somewhat like Comparable<? super T>)
> instead.

So, I tried this...

class BST<T extends Comparable>
{
    class Node<T>
    {
        T obj = null;
        Node left = null, right = null;

        Node(T obj) {
            this.obj = obj;
        }
    }

    Node root = null;
    int nElements = 0;

    public void insert(T obj)
    {
        root = insert(obj,root);
        ++nElements;
    }

    private Node insert(T obj, Node node)
    {
        if( node == null )
            node = new Node(obj);
        else if( obj.compareTo(node.obj) < 0 )
            node.left = insert(obj,node.left);
        else
            node.right = insert(obj,node.right);
        return node;
    }

}

But I get the same warnings.  If both obj and node.obj are of the same
type, why is it complaining? Adding more <T>'s just adds more
warnings... I'm not really sure how this works.  What is the proper
syntax?
Mark - 03 Nov 2006 06:48 GMT
I seem to have solved the problem, but I really have no idea what I
did.  Could someone explain what the <? super T> part means?

(code below if interested)

class BST<T extends Comparable<? super T>>
{
    class Node
    {
        T obj = null;
        Node left = null, right = null;

        Node(T obj) {
            this.obj = obj;
        }
    }

    Node root = null;
    int nElements = 0;
    int index = 0;

    public void insert(T obj)
    {
        root = insert(obj,root);
        ++nElements;
    }

    private Node insert(T obj, Node node)
    {
        if( node == null )
            node = new Node(obj);
        else if( obj.compareTo(node.obj) < 0 )
            node.left = insert(obj,node.left);
        else
            node.right = insert(obj,node.right);
        return node;
    }

   ...
Mark - 03 Nov 2006 07:12 GMT
Writing it as follows seems to fix the problem

class BST<T extends Comparable<? super T>>
{
    class Node
    {
        T obj = null;
        Node left = null, right = null;

        Node(T obj) {
            this.obj = obj;
        }
    }
     ...

But now I'm having trouble declaring a new BST.  I get the errors...

BST.java:196: unexpected type
found   : int
required: reference
        BST<int> tree = new BST<int>();
                   ^
BST.java:196: unexpected type
found   : int
required: reference
        BST<int> tree = new BST<int>();
                                       ^
2 errors

> Terminated with exit code 1.

I'm not sure what it means...it expects a reference?  Why won't it
accept "int"?

*quick test*

It seems to accept Integer though...why is that?
Hendrik Maryns - 03 Nov 2006 12:57 GMT
Mark schreef:
> Writing it as follows seems to fix the problem
>
[quoted text clipped - 29 lines]
> I'm not sure what it means...it expects a reference?  Why won't it
> accept "int"?

int is a primitive type, it does not implement anything, so most
certainly not Comparable.  The moment you generified BST, you demanded
its objects to implement Comparable.  Anyway, there is not way to store
an int in a collection, so you want to use Integer anyway.

> It seems to accept Integer though...why is that?

Because Integer implements Comparable<Integer>.

To answer your other post:

> Could someone explain what the <? super T> part means?

Comparable itself is generic.  This means you have to give it a
parameter, hence the errors didn’t disappear at your first try, since
you just declared BST<T extends Comparable>.

Now why the super T?  Often, a class SuperClass will declare it
implements Comparable.  Most often, it will be comparable with itself,
so it will implement Comparable<SuperClass>.  (E.g. look at the
definition of Integer.)  However, if subclasses of this class are made,
they, too, implement Comparable<SuperClass>.  You cannot redefine them
to implement Comparable<SubClass>, the compiler won’t let you.  But you
do want to accept these classes in BST.  That’s where the super comes
in: it tells the compiler that a class should be comparable to itself or
to some class higher up in the hierarchy.

HTH,H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Mark - 07 Nov 2006 07:27 GMT
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 76 lines]
> =CDaC
> -----END PGP SIGNATURE-----

ah... thank you.

I found the integer definition, it says

public final class Integer
extends Number
implements Comparable

not Comparable<Integer>, .. actually.. I guess those doc's are outdated
now (1.4.2)
generics were introduced in 1.5 or something, weren't they?

anyways, that makes much more sense now.

the broad statement "everything is derived from Object" led me to
believe that even int's were.. and if they were derived from objects, I
figured they might implement Comparable as well, but I guess I was
wrong. (or are they still considered Objects?)
Hendrik Maryns - 07 Nov 2006 10:21 GMT
Mark schreef:
> Mark schreef:
>>>> Writing it as follows seems to fix the problem
[quoted text clipped - 55 lines]
>
> HTH,H.

> ah... thank you.

> I found the integer definition, it says

> public final class Integer
> extends Number
> implements Comparable

> not Comparable<Integer>, .. actually.. I guess those doc's are outdated
> now (1.4.2)
> generics were introduced in 1.5 or something, weren't they?

Indeed, you must be looking in the wrong place.  If you have the Sun
JDK, then you have the source code too (search for src.zip), have a look
there.

> anyways, that makes much more sense now.

> the broad statement "everything is derived from Object" led me to
> believe that even int's were.. and if they were derived from objects, I
> figured they might implement Comparable as well, but I guess I was
> wrong. (or are they still considered Objects?)

Nop, everything is derived from Object, except primitive types.  Whether
this is a good design decision is another discussion...  (E.g. in
Eiffel, even integers are objects.)

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Mark - 09 Nov 2006 10:23 GMT
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 102 lines]
> =kJKS
> -----END PGP SIGNATURE-----

haha... yes, better not get me stirred up about that :)
Thomas Weidenfeller - 02 Nov 2006 11:07 GMT
> i'm serious.. I need help.

Really? I am sure we all thought it was just a joke.

http://moinmoin.riters.com/JINX/index.cgi/Suggestions_20for_20Asking_20Questions
_20on_20Newsgroups

http://moinmoin.riters.com/JINX/index.cgi/Suggestions_20for_20Asking_20Questions
_20on_20Newsgroups#Urgent

http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

/Thomas
Signature

The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Mark - 03 Nov 2006 06:10 GMT
> > i'm serious.. I need help.
>
[quoted text clipped - 9 lines]
> http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
> ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Sorry, I guess I got a little impatient.  I skimmed through the article
and bookmarked it.
I tried using generics, but was unsuccessful.  Perhaps I don't fully
understand them yet, but I just wanted a quick fix for now.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.