Does Java have mutable types for Integer, Float, Double, Long?

JavaNumbersIntegerMutableBuilt In

Java Problem Overview


I am in a situation where I want to use mutable versions of things like Integer. Do I have to use these classes (below) or does Java have something built in?

http://www.java2s.com/Code/Java/Data-Type/Amutableintwrapper.htm

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

This is a thread safe mutable integer, example of use:

final AtomicInteger value = new AtomicInteger(0);

then later on:

value.incrementAndGet();

Solution 3 - Java

No, Java doesn't have these built in. And that is for a reason. Using mutable types is dangerous, as they can easily be misused. Additionally, it is really easy to implement it. For example, commons-lang has a MutableInt.

Solution 4 - Java

Here's a small class I made for a mutable integer:

public class MutableInteger {
    private int value;
    public MutableInteger(int value) {
        this.value = value;
    }
    public void set(int value) {
        this.value = value;
    }
    public int intValue() {
        return value;
    }
}

You could easily extend this to any other primitive. Of course, like everyone else is saying, you should use it carefully.

Solution 5 - Java

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.

Solution 6 - Java

AtomicInteger has already been mentioned. Mutable Doubles can be emulated with AtomicReference<Double>. The already mentioned warnings apply and it is bad style, but sometimes you have code like this

double sum=0
for (Data data:someListGenerator())
  sum+=data.getValue()

and want to refactor it in functional Java 8 style. If the code follows this pattern but adds considerable complexity to it, the most sensible conversion could be

AtomicReference<Double> sumref=new AtomicReference<>(0d);
someStreamGenerator().forEach(data->
  sumref.set(sumref.get().doubleValue()+data.getValue()));
double sum=sumref.get().doubleValue();

Of course, this is at least questionable style. But I found myself more than once in a situation with a twisted loop over a ResultSet computing and partly cumulating three different information from it. This makes it really hard to convert the code into proper functional style. Converting the cumulating parts according to the above pattern seemed to me a reasonable tradeoff between clean code and oversimplified refactoring.

Solution 7 - Java

You can import the org.omg.CORBA package(or just the class you need) and in it you can use the Holder classes.

For example, it has the "IntHolder" where the field where it stores the integer is public, giving access to modify it.

public static void triple(IntHolder x){
	x.value = 3 * x.value;
}

IntHolder mutableInt = new IntHolder(10);
triple(mutableInt);		
System.out.println(mutableInt.value);

It also has "LongHolder" and "DoubleHolder" and tons of others that you can use. Use with caution.

Here is the api for it: https://docs.oracle.com/javase/7/docs/api/org/omg/CORBA/package-summary.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsmuggledPancakesView Question on Stackoverflow
Solution 1 - JavaAlex JasminView Answer on Stackoverflow
Solution 2 - JavaCliveView Answer on Stackoverflow
Solution 3 - JavaBozhoView Answer on Stackoverflow
Solution 4 - JavaVinceFiorView Answer on Stackoverflow
Solution 5 - JavaPeter LawreyView Answer on Stackoverflow
Solution 6 - JavaDirk HillbrechtView Answer on Stackoverflow
Solution 7 - JavaErkan UretenerView Answer on Stackoverflow