0

I have a situation where I have several functions that get passed in different types (float, int, double, etc). Currently I am boxing these types into Float, Integer, Double, etc, and passing them into one function which takes an Object parameter. Similar to the following:

private void handleInt(int val) {
    //do something
    addValue(new Integer(val));
}

private void handleFloat(float val) {
    //do something
    addValue(new Float(val));
}

private void handleDouble(double val) {
    //do something
    addValue(new Double(val));
}

private void addValue(Object val) {
    //Do something with object val
    //.compareTo needs to be called here to compare previous value
}

I would like to compare the val parameter sent to addValue with a previous one I store in an array. However, because Object is a base class of all of the different boxed types (Float, Integer, etc), do I need to write my own class that extends Object and implements comparable?

And in my compareTo function, how would I accomplish this?

Hans Landa
  • 95
  • 1
  • 9
  • 1
    You don't need to construct the wrappers explicitly. Jus do `addValue(val);`. Autoboxing will handle the rest. – shmosel May 09 '17 at 17:12
  • 1) Why do you need to compare *relative*, i.e. less than / greater than? Why is `equals()` not enough? All objects implement equals, and can compare disparate types. --- 2) If you *must* compare relative, when how to you compare a previous value of type `String` against a new value of type `Double`? – Andreas May 09 '17 at 17:12
  • 1
    Agree with @shmosel, but if you prefer to be explicit, you should use `valueOf()`, rather than `new`, e.g. `addValue(Integer.valueOf(val));`, which is exactly what autoboxing does. – Andreas May 09 '17 at 17:14
  • I would like to compare, because I want to check which one is greater than or less than, rather than if they just equal each other. – Hans Landa May 09 '17 at 17:15
  • @HansLanda So how do you want to handle my second point? – Andreas May 09 '17 at 17:16
  • @Andreas The entire purpose of using an Object is so I reduce a lot of duplicate code. I want to only compare Double to Double and Float to Float. Never do I want to compare a Double to a String. I just want one function that takes all different Boxed types and compares their values to an Object[] Array – Hans Landa May 09 '17 at 17:20
  • More specific, the function takes an Object, compares it to a specific object in a hash table and overwrites if larger. – Hans Landa May 09 '17 at 17:23
  • If you know the type but you're trying to reduce duplication, you're using the wrong tools. Look into generics. – shmosel May 09 '17 at 17:24
  • unfortunately, i'm using ibm's j9 which doesn't support generics, lol. :( – Hans Landa May 09 '17 at 17:26
  • Sorry to hear that. In your case I would just cast them to `Comparable` so you can call `compareTo()`. – shmosel May 09 '17 at 17:30
  • Don't I need to explicitly define the compareTo function? – Hans Landa May 09 '17 at 17:32
  • No, it's already defined in each number class. – shmosel May 09 '17 at 17:35
  • You're a genius, thanks. – Hans Landa May 09 '17 at 17:39

0 Answers0