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

不打扰是莪最后的温柔 提交于 2019-11-27 21:45:48
Bozho

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.

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.

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();

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!