aspose file tools
The moose likes Java in General and the fly likes interface Comparable vs. equals() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "interface Comparable vs. equals() method" Watch "interface Comparable vs. equals() method" New topic

interface Comparable vs. equals() method

James Brooks
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Sorry, same question, but slightly different topic, so thought I'd start a new thread. Is one better than the other for any reason? My book does not offer an opinion. Thanks!

Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Comparable is an interface:public int compareTo(T o);

You write that when you write some class, or you assure that it is present in some class you are going to store in an ordered collection.

the .equals method is used where == won't work, the double equals (==) simple gives the reference address of an object, it will work on primitives such as integer, double and whatever. It will not work on anything that is an instance of a class, at least on in the traditional sense, instanceof is often used where one would initially consider ==

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Have you tried LearnNowOnline?
subject: interface Comparable vs. equals() method