Implement a mutable int class in Java
This post will discuss how to implement a mutable int class in Java, which is a mutable wrapper over primitive int. We will also take a look at MutableInt class provided by Apache Commons Library.
A mutable class in Java is a class whose instances can be modified after they are created. It means that the state of the object can change through various methods and operations during its lifetime. A MutableInt class is a class that wraps a primitive int value and allows it to be changed. We can either implement a custom mutable int class in Java, or use the implementation provided by the several third-party libraries.
1. Custom Implementation
We can create our own mutable int class by defining a private int value and providing public methods to access and modify it. We can also override the toString() method to return the corresponding string value of the int field. By implementing our own custom solution instead of relying on third-party libraries, we can reduce the number of external dependencies in our app and have more flexibility to add more methods as needed. Here is an example of how the custom MutableInt class might look like in Java:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
class MutableInt { private int value; MutableInt() { // the default value is 0 this.value = 0; } MutableInt(int value) { // initialize with the specified value this.value = value; } public void increment() { // increment value by 1 value++; } public int getAndIncrement() { // increment value by 1 and return the previous value return value++; } public int incrementAndGet() { // increment value by 1 and return return ++value; } public void decrement() { // decrement value by 1 value--; } public int getAndDecrement() { // decrement value by 1 and return the previous value return value--; } public int decrementAndGet() { // decrement value by 1 and return return --value; } public void add(int operand) { // add operand value to the value of this instance value += operand; } public void remove(int operand) { // remove operand value from the value of this instance value -= operand; } public int getValue() { // return value of this instance as int return value; } public void setValue(int value) { // set value of this instance to specified int value this.value = value; } @Override public String toString() { // returns the corresponding string value of this mutable return String.valueOf(getValue()); } } class Main { public static void main(String[] args) { MutableInt i = new MutableInt(); i.increment(); i.add(9); i.remove(4); System.out.println(i); // 6 } } |
2. Using Apache Commons Lang
The Apache Commons Lang library provides an implementation of the MutableInt class, which has similar methods as the custom implementation, as well as some additional methods such as intValue(), toInteger(), doubleValue(), equals(), hashCode(), and compareTo(). The basic usage of this class is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import org.apache.commons.lang3.mutable.MutableInt; class Main { public static void main(String[] args) { // Construct a `MutableInt` with the default value of zero MutableInt i = new MutableInt(); i.increment(); // increment value by 1 i.add(10); // add 10 i.subtract(9); // subtract 9 i.decrement(); // decrement value by 1 System.out.println(i); // print the current value, which is 1 // Construct a new `MutableInt` with the specified value MutableInt j = new MutableInt(10); // Convert `MutableInt` to different data types int k = j.intValue(); Integer l = j.toInteger(); String s = j.toString(); Double d = j.doubleValue(); // other operations } } |
Note that Guava library provides an implementation of the AtomicInteger class, which is a thread-safe mutable int that supports atomic operations. It has similar methods as the Apache Commons Lang MutableInt class, as well as some additional methods such as getAndIncrement(), incrementAndGet(), getAndDecrement(), decrementAndGet(), getAndAdd(), addAndGet(), compareAndSet(), and getAndUpdate().
That’s all about implementing a mutable int class in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)