Java is pass by value and it is not possible to pass integer by reference in Java directly. Objects created in Java are references which are passed by value. Thus it can be achieved by some methods which are as follows:
- By creating Wrapper Class: As we know that Integer is an immutable class, so we wrap an integer value in a mutable object through this method.
Approach:
- Get the integer to be passed
- Create an object of another class with this integer
- Using wrapper class so as to wrap integer value in mutable object which can be changed or modified
- Now whenever you need the integer, you have to get it through the object of the class
- Hence the Integer has been passed by reference
Below is the implementation of the above approach:
Example:
// Java program to pass the integer by referenceÂÂclassTest {   ÂpublicInteger value;   ÂTest(Integer value)   Â{       Â// Using wrapper class       Â// so as to wrap integer value       Â// in mutable object       Â// which can be changed or modified       Âthis.value = value;   Â}   Â@Override   ÂpublicString toString()   Â{       ÂreturnString.valueOf(value);   Â}}ÂÂclassMain {   Âpublicstaticvoidmodification(Test x)   Â{       Âx.value =1000;   Â}   Âpublicstaticvoidmain(String[] args)   Â{       ÂTest k =newTest(50);       Âmodification(k);       Â// Modified value gets printed       ÂSystem.out.println(k);   Â}}Output:
1000
- Wrapping primitive value using an array: This process of wrapping is done by using an array of length one.
Approach:
- Get the integer to be passed
- This process of wrapping is done by using an array of length one.
- Now whenever you need the integer, you have to get it through the object of the array
- Hence the Integer has been passed by reference
Below is the implementation of the above approach:
Example:
// Java program to pass the integer by referenceÂÂclassPassByReference {   Âpublicstaticvoidincrement(int[] array)   Â{       Â// increment in the actual value       Âarray[0]++;   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Âintk =100;       Â// wrapping is done       Â// by using array of length one       Âint[] array = { k };       Â// Reference is passed       Âincrement(array);       Â// incremented value printed       ÂSystem.out.println(array[0]);   Â}}Output:
101
- Using AtomicInteger: This is a built in Java class which provides a single threaded environment.
Approach:
- Get the integer to be passed
- Create an AtomicInteger object by passing this integer as parameter to its constructor
- Now whenever you need the integer, you have to get it through the object of the AtomicInteger
- Hence the Integer has been passed by reference
Below is the implementation of the above approach:
Example:
// Java program to pass the integer by referenceÂÂimportjava.util.concurrent.atomic.AtomicInteger;ÂÂclassPassByReference {   Âpublicstaticvoidsetvalue(AtomicInteger x)   Â{       Â// setting new value       Â// thus changing the actual value       Âx.set(1000);   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// provides single threaded environment       ÂAtomicInteger k =newAtomicInteger(50);       Â// passed by reference       Âsetvalue(k);       ÂSystem.out.println(k);   Â}}Output:
1000
- Using Apache MutableInt Class: We can use MutableInt class by importing the package from Commons Apache.
Approach:
- Get the integer to be passed
- Create an MutableInt object by passing this integer as parameter to its constructor
- Now whenever you need the integer, you have to get it through the object of the MutableInt
- Hence the Integer has been passed by reference
Below is the implementation of the above approach:
Example:
// Java program to pass the integer by referenceÂÂimportorg.apache.commons.lang3.mutable.MutableInt;ÂÂclassMain {   Âpublicstaticvoidincrement(MutableInt k)   Â{       Âk.increment();   Â}   Âpublicstaticvoidmain(String[] args)   Â{       Â// Using Mutable Class whose object's fields       Â// can be changed accordingly       ÂMutableInt k =newMutableInt(8);       Âincrement(k);       ÂSystem.out.println(k);   Â}}Output:
9
