wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes CompareTo method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "CompareTo method" Watch "CompareTo method" New topic
Author

CompareTo method

Beckie Wirefly
Greenhorn

Joined: Nov 21, 2008
Posts: 2
Hello,

This is my first post, so I hope I am doing this correctly. I am studying Programming at University (College) and am a little stuck on the CompareTo method in the Comparable interface.


What I need to do is:
If the price is equal (return 0), then compare the area's.

I hope this is clear enough, here is my code:




I believe I am doing the correct thing, but not too sure. I think the problem may lay in comparing two objects???

Thank you for your insight.

Beckie
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40


Line 61 and after, of the compareTo() method, should be unreachable -- as it is not possible to get pass the if-then statement without returning from the method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Beckie Wirefly
Greenhorn

Joined: Nov 21, 2008
Posts: 2
Henry Wong wrote:
Line 61 and after, of the compareTo() method, should be unreachable -- as it is not possible to get pass the if-then statement without returning from the method.

Henry


Of course, thank you for correcting that for me!!

Beckie
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18


That only returns true if o is an Integer object, but at that point (should it be reached by removing a previous return statement) you already know that o is a House object - or the cast on line 54 would already have failed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: CompareTo method
 
Similar Threads
about generics
Explain Working of compareTo() method with respect to this example
Still not getting working of compareTo() method
Still trying to understand compareTo() method
Error can't find class