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
class
Test {
public
Integer 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
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);
// 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
class
PassByReference {
public
static
void
increment(
int
[] array)
{
// increment in the actual value
array[
0
]++;
}
public
static
void
main(String[] args)
{
int
k =
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
import
java.util.concurrent.atomic.AtomicInteger;
class
PassByReference {
public
static
void
setvalue(AtomicInteger x)
{
// setting new value
// thus changing the actual value
x.set(
1000
);
}
public
static
void
main(String[] args)
{
// provides single threaded environment
AtomicInteger k =
new
AtomicInteger(
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
import
org.apache.commons.lang3.mutable.MutableInt;
class
Main {
public
static
void
increment(MutableInt k)
{
k.increment();
}
public
static
void
main(String[] args)
{
// Using Mutable Class whose object's fields
// can be changed accordingly
MutableInt k =
new
MutableInt(
8
);
increment(k);
System.out.println(k);
}
}
Output:
9