Open In App

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:

  1. 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:

    1. Get the integer to be passed
    2. Create an object of another class with this integer
    3. Using wrapper class so as to wrap integer value in mutable object which can be changed or modified
    4. Now whenever you need the integer, you have to get it through the object of the class
    5. 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
  2. Wrapping primitive value using an array: This process of wrapping is done by using an array of length one.

    Approach:

    1. Get the integer to be passed
    2. This process of wrapping is done by using an array of length one.
    3. Now whenever you need the integer, you have to get it through the object of the array
    4. 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
  3. Using AtomicInteger: This is a built in Java class which provides a single threaded environment.

    Approach:

    1. Get the integer to be passed
    2. Create an AtomicInteger object by passing this integer as parameter to its constructor
    3. Now whenever you need the integer, you have to get it through the object of the AtomicInteger
    4. 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
  4. Using Apache MutableInt Class: We can use MutableInt class by importing the package from Commons Apache.

    Approach:

    1. Get the integer to be passed
    2. Create an MutableInt object by passing this integer as parameter to its constructor
    3. Now whenever you need the integer, you have to get it through the object of the MutableInt
    4. 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

Article Tags :