doing arithmetic on Integer objects
Is there a way to do arithmetic on Integer objects? For example, I tried the following without success:
Integer I = new Integer(1);
I++;
I also noticed there is no setValue(int) method in the Integer class, so I can't do this:
I.setValue(I.intValue() + 1);
The only way I can think of to change the value of an Integer (or any Number object) through arithmetic is to create a new one based on the value of the old one. So something like this:
I = new Integer(I.intValue() + 1);
Is this really the only way to do it?
Re: doing arithmetic on Integer objects
Any reason you must rely on an Integer object rather than the primitive? The Integer class is is a wrapper class whose value is final, so to modify it you must create a new Integer object derived from the previous, or use primitives.
Re: doing arithmetic on Integer objects
copeg is absolutely correct, but you could also do this:
Google autoboxing.
Re: doing arithmetic on Integer objects
Quote:
Originally Posted by
KevinWorkman
copeg is absolutely correct, but you could also do this:
Google autoboxing.
Well, the compiler is happy with this.
I'm using Integers because I'm placing them in a Vector.
Re: doing arithmetic on Integer objects
It seems this *can* work (depending on your application), but one should know this operation creates a new Integer object out of the old. So if you had any other references to the old one, they will not point to the new one.