How to pass integer by reference in Java
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 referenceclassTest {publicInteger value;Test(Integer value){// Using wrapper class// so as to wrap integer value// in mutable object// which can be changed or modifiedthis.value = value;}@OverridepublicString toString(){returnString.valueOf(value);}}classMain {publicstaticvoidmodification(Test x){x.value =1000;}publicstaticvoidmain(String[] args){Test k =newTest(50);modification(k);// Modified value gets printedSystem.out.println(k);}}chevron_rightfilter_noneOutput:
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 referenceclassPassByReference {publicstaticvoidincrement(int[] array){// increment in the actual valuearray[0]++;}publicstaticvoidmain(String[] args){intk =100;// wrapping is done// by using array of length oneint[] array = { k };// Reference is passedincrement(array);// incremented value printedSystem.out.println(array[0]);}}chevron_rightfilter_noneOutput:
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 referenceimportjava.util.concurrent.atomic.AtomicInteger;classPassByReference {publicstaticvoidsetvalue(AtomicInteger x){// setting new value// thus changing the actual valuex.set(1000);}publicstaticvoidmain(String[] args){// provides single threaded environmentAtomicInteger k =newAtomicInteger(50);// passed by referencesetvalue(k);System.out.println(k);}}chevron_rightfilter_noneOutput:
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 referenceimportorg.apache.commons.lang3.mutable.MutableInt;classMain {publicstaticvoidincrement(MutableInt k){k.increment();}publicstaticvoidmain(String[] args){// Using Mutable Class whose object's fields// can be changed accordinglyMutableInt k =newMutableInt(8);increment(k);System.out.println(k);}}chevron_rightfilter_noneOutput:
9
Recommended Posts:
- How to pass PHP Variables by reference ?
- Perl | Pass By Reference
- Java is Strictly Pass by Value!
- Program to Convert Set of Integer to Array of Integer in Java
- 'this' reference in Java
- Java.lang.Integer class in Java
- Integer sum() Method in Java
- Integer toString() in Java
- How to pass JavaScript variables to PHP ?
- Integer reverseBytes() Method in Java
- Integer rotateLeft() Method in Java
- Integer intValue() Method in Java
- Integer reverse() Method In Java
- Integer highestOneBit() Method in Java
- Integer.numberOfLeadingZeros() Method in Java With Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



