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:
class Test {
public Integer value;
Test(Integer value)
{
this .value = value;
}
@Override
public String toString()
{
return String.valueOf(value);
}
}
class Main {
public static void modification(Test x)
{
x.value = 1000 ;
}
public static void main(String[] args)
{
Test k = new Test( 50 );
modification(k);
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:
class PassByReference {
public static void increment( int [] array)
{
array[ 0 ]++;
}
public static void main(String[] args)
{
int k = 100 ;
int [] array = { k };
increment(array);
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:
import java.util.concurrent.atomic.AtomicInteger;
class PassByReference {
public static void setvalue(AtomicInteger x)
{
x.set( 1000 );
}
public static void main(String[] args)
{
AtomicInteger k = new AtomicInteger( 50 );
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:
import org.apache.commons.lang3.mutable.MutableInt;
class Main {
public static void increment(MutableInt k)
{
k.increment();
}
public static void main(String[] args)
{
MutableInt k = new MutableInt( 8 );
increment(k);
System.out.println(k);
}
}
|
Output:
9
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
09 May, 2019
Like Article
Save Article