comparable interface and compareTo method

Posted on 2006-04-30
Last Modified: 2008-03-10
You use the compareTo method to compare two objects right? How come sometimes before comparing the objects it is necessary to type cast them to type Comparable?  (Comparable)obj
Question by:yattias
    LVL 17

    Accepted Solution

    Hi yattias,

    Could you show the situation where this is happening for you?

    If you accept the object as:
    Object obj;
    Then you know nothing of that object's methods but the most basic.  You are uncertain if that has a compareTo() method at all.  This can be improved with generics.  Ensure that the object must extend Comparable and then the compiler would already know that hte object has a compareTo method.  Same if instead of doing Object obj you did Comparable obj.  Just if it was defiend as an Object then the compiler does not know any information about that object except the basics.

    Joe P
    LVL 23

    Expert Comment


    Just check out all the 3 parts.

    I don't think that kind of Type casting is required as many classes already implement if for exacmple check out String Class it a Comparable Object.

    Author Comment

    thanks, what I meant was really in general, why would you type cast an object to Comparable? is it so it will include the comparable interface for incase it does not?
    LVL 17

    Expert Comment

    > why would you type case an object to Comparable
    To ensure that the object your casting has a compareTo method.

    Look at the Object class definition.  It has what, 10 methods?
    wait, clone, toString, notifyAll, notify, hashCode, getClass, finalize, equals

    None of those are compareTo.  If you do this:
    Object obj = new Object();
    obj.compareTo(new Object());

    Then the compiler will say I don't see a compareTo method.  You have a problem!
    Type casting it to a Comparable ensures the compiler that the compareTo is defined in the object.
    Joe P

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now