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

I am in a situation where I want to use mutable versions of things like Integer. Should I use these classes (see below) or is there something built in to Java?

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

+52
java integer numbers mutable built-in
Dec 23 '10 at 15:35
source share
7 answers

No, Java does not have these built-in. And this is for some reason. Using mutable types is dangerous because they can easily be used incorrectly. Moreover, it is really easy to implement. For example, commons-lang has a MutableInt .

+35
Dec 23 '10 at 15:39
source share

You can always transfer a value to an array, for example int[] mutable = {1}; if you include the code for a mutable wrapper class too cumbersome.

+51
Dec 23 '10 at 15:38
source share

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

This is a variable integer, usage example:

 public void incrementInt(AtomicInteger value) { value.set(value.intValue()+1); } 
+29
Aug 13 '12 at 1:01
source share

You can use 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 changes.

Can you get more details on why you need a mutliple object, maybe there is another way to achieve the same.

+8
Dec 23 '10 at 15:47
source share

Here is a small class that 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 can easily extend this to any other primitive. Of course, like everyone else, you should use it carefully.

+7
Jul 31 '14 at 4:43
source share

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

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

and you want to reorganize it in the functional style of Java 8. If the code follows this template, but gives it more complexity, the most reasonable transformation may 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 a dubious style. But I found myself more than once in a twisted-loop situation over calculating a ResultSet and partially cumulating three different data. This makes it difficult to convert the code to the correct functional style. Converting the cumulative parts in accordance with the above example seemed to me a reasonable compromise between clean code and simplified refactoring.

+5
Aug 14 '16 at 10:08 on
source share

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

For example, it has "IntHolder", where the field in which the integer is stored is public, providing access to change 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 many more 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

+1
Jan 07 '16 at 5:21
source share



All Articles