Hello all, it's been a while since I've had a question, but I'm back.

I'm trying to write a Vector class which can be any type. The problem I have is that generics can only hold objects, so when I try to say x+=vec.x it says that it cannot do that, I tried to make sure that it extends number, but the autoboxing doesn't seem to work with generics.

can anyone help?

Can we see part of the code?

You might also explain what you mean by "cannot do that".

And explain exactly what "vec.x" is, as that is not how you access something in a vector.

But, of course, don't forget the actual code.

what i mean by "cannot do that" is that because of the generic within the class trying to take the component x of type <T> and add it to another Java won't accept that as correct.
here is the code that is causing the problem

package cannonfodder;


/**
 * @author Bill
 * @param <T> type
 */
public class Vector <T extends Number> {
    T x, y;
    /**
     * @param x x
     * @param y y
     */
    public Vector(T x,T y) {
        this.x=x;
        this.y=y;
    }
    /**
     * @param x x
     */
    public void setX(T x) {
        this.x=x;
    }
    /**
     * @param y y
     */
    public void setY(T y) {
        this.y=y;
    }
    /**
     * @param x x
     * @param y y
     */
    public void set(T x, T y) {
        this.x=x;
        this.y=y;
    }
    /**
     * @param vec vector
     */
    public void add(Vector<T> vec) {
        x+=vec.x;
        y+=vec.y;
    }
    /**
     * @param vec vector
     * @return dot
     */
    public double dot(Vector vec) {
        return x*vec.x+y*vec.y;
    }
    /**
     * @param vec vector
     */
    public void sub(Vector vec) {
        x-=vec.x;
        y-=vec.y;
    }
    /**
     * @param d d
     */
    public void mult(double d) {
        x*=d;
        y*=d;
    }
}

The + operator only works with certain types. If you aren't using it with those types, then it isn't going to work. If you want more detailed reasoning on this, masijade is probably your man. But you should try using the floatValue method of the Number class, then adding the floats you get. But for example, Number could be a BigDecimal, BigInteger, etc (look at the doc) - which can't be added as simply as an int.

Nope, it can't, mainly because Number itself can't use those operators, only a little over half the classes that extend Number can use those operators (only through autoboxing of course, which Number, of course, also can't do).

The compiler can only work with what it knows it has, and in this case, that is "Number", and "Number", as already mentioned, cannot use those operators (nor can it be autoboxed, of course).

You can still "get away with" some of this by making this an abstract class and making the last three methods abstract, otherwise you will have to implement a somewhat involved case statement in those methods.

Thanks, perhaps I'll just make it for only one type. The reason I wanted this is because I wanted to have a position in an array as a vector which has to be an int, but I also wanted to be able to have a vector that was in doubles for things like forces velocity and acceleration. but I suppose I will either have to make due with one, or write both separately. Thanks for the info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.