java.lang.Number doesn't implement “+” or any other operators?

后端 未结 7 1666
梦毁少年i
梦毁少年i 2020-12-17 19:56

I\'m creating a class which is supposed to be able to be used with an array of any type of number (float, int, etc), so here is one method I have:

// T exten         


        
相关标签:
7条回答
  • 2020-12-17 20:10

    Sadly, you can't. Arithmetic operators work only on primitive types (and thanks to autoboxing and autounboxing on their wrappers). You have to override the given method for all the primitive types that you require the method to work on, as is done in many JDK classes.

    0 讨论(0)
  • You're misunderstanding the way numbers work in Java. The Number class is the superclass of numeric wrapper classes (Integer, Float, etc.) useful for representing primitive types (int, float, etc.) as objects, but it does not work with the usual arithmetic operators.

    If you intend to use the arithmetic operators, then use primitive types. If you need to build a "generic" method that works for all numeric data types, you have no choice but to build several overloaded versions of the same method, one for each data type, for example:

    public  float[] average(float[][]  queue) {...}
    public double[] average(double[][] queue) {...}
    

    Also be aware that code like this appears to work for wrapper types:

    Integer i = 0;
    i += 1;
    System.out.println(i);
    

    ... But under the hood, Java is automatically boxing and unboxing the Integer, since the += operator only works for primitive types. It works because we're explicitly indicating that the number is an Integer, but it won't work for a Number, since Java needs to know exactly what type of number it's dealing with for performing the boxing/unboxing.

    0 讨论(0)
  • 2020-12-17 20:19

    I think this is what you want

    public synchronized average() {  
        double ret = new double[queue[0].length];  
        for (int i = 0; i < ret.length; ++i) {  
            for (int j = 0; j < size; ++j) {  
                ret[i] += queue[j][i];  
            }  
            ret[i] /= size;  
        }  
        return ret;  
    }
    
    0 讨论(0)
  • 2020-12-17 20:19
    public int showAllNum(Number i, Number j) {
        return (int) (i.doubleValue()+j.doubleValue()); 
    }
    
    0 讨论(0)
  • 2020-12-17 20:28

    In Java, the arithmetic operators will only work on primitive types. The problem here is that Java has class representations of these primitive types, and the switch from one to another is generally implicit, via the feature known as autoboxing.

    In this case you will need to implement methods for the types of arithmetic operations you need, possibly having to create overloaded methods for every possible numeric type passed in for every operation.

    0 讨论(0)
  • 2020-12-17 20:34

    Since every possible byte, short, char, int, float, double can be represented as a double its is much more efficient (as its a primitive instead of any object) and simpler to use double instead of Number You would need specific types if you need long accurately or BigDecimal or BigInteger.

    0 讨论(0)
提交回复
热议问题