java - Why I can't use + operator with Integer and Double in generic method? -
i encountered strange issue. don't understand why code doesn't work:
public static <type1 extends integer, type2 extends double > object addition(type1 first, type2 second) { return second * first; }
the compiler claims operator * cannot applied type2,type1
. types extends integer , double has + operator defined them. don't understand this
you wrong. neither integer nor double or other objects extending number class have "+" or other operator definied. reason why able perform (integer + double) autoboxing. , autoboxing kind of "hardcoded" feature applies small predefined set of classes, such integer, double, etc.
furthermore, integer , other number subclasses declared "final", therefore "type extends integer" has no meaning, since cannot extend integer.
Comments
Post a Comment