1 Reply Latest reply on Feb 25, 2007 10:34 AM by 807606

    compareto method


      i have a method :

      checkNumberOperation(String operation,Object value, Object v1).

      value & v1 are both can be Integer,Double or Long. (always the same


      as you know the method "compareto" can be used on all of those three

      Objects (/classes)

      i wanna know how can i use value.compareto(v1) without casting every time the value object.
        • 1. Re: compareto method
          You can just cast it to Comparable. You're still casting, but you don't have to cast to the specific concrete type, so it should simplify your code.

          This should be intuitive when you think about it. You give the method some object, about which the method knows little. But you happen to know that, whatever the object is, it implements Comparable. So by casting it as such, you're telling the method that.

          That's just an anthropomorphic analogy but maybe it helps.

          Message was edited by: