Take the 2-minute tour ×



share|improve this question
The question is why do you want to do this? –  helpermethod Dec 23 '10 at 15:41
add comment

4 Answers

up vote 21 down vote accepted


share|improve this answer
My guess is the java dev's wanted Integer to 'behave' like an int, which is..once you have a reference to it, it never changes (avoid confusion). But..it still seems odd to not have a mutable option somehow, to me... –  rogerdpack Feb 11 '13 at 23:02
add comment

You could always wrap the value in an array like int[] mutable = {1}; if including the code for a mutable wrapper class is too cumbersome.

share|improve this answer
Smart, bug ugly as sin. –  Susei May 15 at 16:40
add comment

Since JDK 1.5 java now has java.util.concurrent.atomic.AtomicInteger

This is a mutable integer, example of use:

public void incrementInt(AtomicInteger value) {
share|improve this answer
I realize you're just providing an example of a mutable integer, but this code should never be used. You're taking the atomic behavior of AtomicInteger and making it non-atomic, leaving yourself with a mutable integer with (probably) lots of unnecessary synchronisation logic. AtomicInteger provides lots of methods to maintain its atomic behaviour: set, incrementAndGet, getAndIncrement, etc. Use those methods instead of this code. –  scompt.com Oct 5 '12 at 14:25
but it is in the stdlib, and it is a mutable integer class...just not exactly what the OP was looking for... –  rogerdpack Feb 11 '13 at 23:00
add comment

You can use an nnnn[] as a mutable object for any primitive type as @Alexandre suggests, java also has AtomicInteger and AtomicLong.

IMHO int is usually a better choice than Integer and that is mutable.

Can you more details of why you need a mutliple object, perhaps there is another way to achieve the same thing.

share|improve this answer
int is not mutable. –  mjaggard Jan 31 '12 at 14:19
int is always mutable unless its also final –  Peter Lawrey Jan 31 '12 at 15:39
It's not really valid to say that a primitive type is mutable. It can be changed but so can every non-final object if you just point to a new object. For example Integer a = 4; then a = 5; is valid code, but Integer is not mutable. –  mjaggard Feb 1 '12 at 9:08
I agree that Integer instances are not mutable even if references to them are, but that's a different type. –  Peter Lawrey Feb 1 '12 at 9:34
Isn't this a good way to have a reference int parameter? For example a function that returns a paginated List of items (from the database), but also the total number of records. In such a case, AtomicInteger or MutableInteger seem to be useful. Of course the other way would be to have a getTotalRecords property instead of returning it in the same method. –  msanjay Apr 24 '12 at 7:21
add comment

Your Answer


