How to pass integer by reference in Java Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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 // 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 // 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 // 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 Comment More infoAdvertise with us Next Article Different Ways to Achieve Pass By Reference in Java K kartikgoel1999 Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Passing Strings By Reference in Java In Java, variables of primitive data types, such as int, char, float, etc., are passed by value, meaning that a copy of the variable's value is passed to a method or function. However, when it comes to passing objects, including Strings, the concept of passing by reference versus passing by value ca 5 min read Different Ways to Achieve Pass By Reference in Java There are two types of parameters one is Formal parameters and the second is Actual Parameters. Formal parameters are those parameters that are defined during function definition and Actual parameters are those which are passed during the function call in other Function. Showcasing the formal and ac 5 min read Pass By Reference In C Passing by reference is a technique for passing parameters to a function. It is also known as call by reference, call by pointers, and pass by pointers. In this article, we will discuss this technique and how to implement it in our C program. Pass By Reference in C In this method, the address of an 4 min read How to Read and Print an Integer Value in Java? The given task is to take an integer as input from the user and print that integer in Java. To read and print an integer value in Java, we can use the Scanner class to take input from the user. This class is present in the java.util package.Example input/output:Input: 357Output: 357Input: 10Output: 3 min read C++ Functions - Pass By Reference In C++, there are different ways to pass data (or variables) to a function, with two common methods being Passing by Value and Passing by Reference. Passing by Reference lets a function modify a variable directly, without creating a copy. The variable and parameter share the same memory location, so 3 min read Integer decode() Method in Java It is often seen that any integer within (" ") is also considered as string, then it is needed to decode that into the integer. The main function of java.lang.Integer.decode() method is to decode a String into an Integer. The method also accepts decimal, hexadecimal, and octal numbers. Syntax : publ 2 min read Like