*
The moose likes Beginning Java and the fly likes What's wrong with my comparTo() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's wrong with my comparTo() method?

Rakoczi Markus
Greenhorn

Joined: Mar 13, 2012
Posts: 13
Hello,

I need to create a Node class for a binary tree with String data, here follows my class



I got the following hints on the commented line

cannot find symbol
symbol: method compareTo(Object)
location: variable data of type String
where String is a type-variable:
String extends Object declared in class Node
----
(Alt-Enter shows hints)


the error at compile time is:
error: cannot find symbol return this.data.compareTo(((Node)_otherNode).getData());
symbol: method compareTo(Object)
location: variable data of type String where String is a type-variable: String extends Object declared in class Node


what's wrong with my code?

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4444
    
    5

Does the superclass of Node have a compareTo(Object) method to override?


Junilu - [How to Ask Questions] [How to Answer Questions]
Rakoczi Markus
Greenhorn

Joined: Mar 13, 2012
Posts: 13
No, the Node class doesn't have any superclass (except the Object), and the getData() method returns a String.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4444
    
    5

Rakoczi Markus wrote:No, the Node class doesn't have any superclass (except the Object), and the getData() method returns a String.

In Java, any class that doesn't explicitly extend another class implicitly extends Object; that is, Object is the parent class of Node. It doesn't matter that getData() returns a String, your method declaration says @Override ... compareTo(Object). So, with this, you should know what the problem is.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I think the problem is this:


That <String> doesn't refer to java.lang.String. Rather, it's a type variable. You're declaring that type variable there. That's exactly the same as if you had just done


Except that now anywhere you use the identifier String it will be referring to your type variable, not to java.lang.String. That is, it's the same as if you did


and so on. And that "T" gets erased to Object.

So change


to


or <E> or whatever you want.

Of course, you're not actually using that anywhere, so you could just get rid of it completely. Or, if you want to store data other than String, you could do


Or something like that.


Other notes:

Don't initialize member varaibles to their default values. (e.g. String data; instead of String data = null;)


Also

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Junilu Lacar wrote:
Rakoczi Markus wrote:No, the Node class doesn't have any superclass (except the Object), and the getData() method returns a String.

In Java, any class that doesn't explicitly extend another class implicitly extends Object; that is, Object is the parent class of Node. It doesn't matter that getData() returns a String, your method declaration says @Override ... compareTo(Object).


I don't think that's the problem, since his call to data.compareTo(...getData()) is a call to String.compareTo(String). See my response above for (what I think is) the real problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Why does a Node have to be compared to another Node in the first place?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Campbell Ritchie wrote:Why does a Node have to be compared to another Node in the first place?


So that we can order Nodes based on the data they hold?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4444
    
    5

Jeff Verdegan wrote:I don't think that's the problem, since his call to data.compareTo(...getData()) is a call to String.compareTo(String). See my response above for (what I think is) the real problem.

I missed the issue with the generics that you pointed out, to be honest. However, the code I pointed out is also a problem. Since the class is declared with implements Comparable<Node>, it will be required to implement compareTo(Node) not compareTo(Object). Additionally, the @Override on his compareTo(Object) method would cause a failed compiler check.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Junilu Lacar wrote:
Jeff Verdegan wrote:I don't think that's the problem, since his call to data.compareTo(...getData()) is a call to String.compareTo(String). See my response above for (what I think is) the real problem.

I missed the issue with the generics that you pointed out, to be honest. However, the code I pointed out is also a problem. Since the class is declared with implements Comparable<Node>, it will be required to implement compareTo(Node) not compareTo(Object). Additionally, the @Override on his compareTo(Object) method would cause a failed compiler check.


Right, that would be a problem as well, but it would have a different symptom than the one he's seeing now.

That darn <existing_class_name> type variable issue has snuck past me a few times in my career. If you're not imitately familiar with generics (and I'm not), it can be tough to spot.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4444
    
    5

Jeff Verdegan wrote:That darn <existing_class_name> type variable issue has snuck past me a few times in my career. If you're not imitately familiar with generics (and I'm not), it can be tough to spot.

I'm no expert in generics either but I tried compiling a version that just uses Node and not Node<T> and got it to compile.

@OP: is there a need to make the data type generic? If not, you can remove the <String> part from the class declaration and with the tweaks that I hinted at, you should be able to get your Node class to compile.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Jeff Verdegan wrote:Right, that would be a problem as well, but it would have a different symptom than the one he's seeing now.

I agree.

Furthermore:
@Rakoczi: In your case, T must also be Comparable, since you use compareTo() to compare your data fields.

Comparable is also a bit of a bugger when it comes to generics, since there's no absolute requirement for comparison to be restricted to a specific type. However, assuming that you do want that restriction, and putting together everything Junilu and Jeff have said, you get:then, if you really feel you need to (although I suspect you don't), you can create a typed subclass, viz:
HIH

Winston

PS: I'd get out of the habit of using underscores in your names if I were you. While not illegal, there's no real need for it; and it marks you out as an "immigrant" from Python or C/C++ (or some other language that uses the style).

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rakoczi Markus
Greenhorn

Joined: Mar 13, 2012
Posts: 13
Thanks for all your replies, it was a great help to me!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome!
 
Consider Paul's rocket mass heater.
 
subject: What's wrong with my comparTo() method?
 
Similar Threads
Dynamic Casting?
Issues with Linked List Add methods
An interview Question.
how to read and show a byte array?
Web Services Performance Problem