| by Arround The Web | No comments

How to Pass by Reference in Java

While dealing with mathematical calculations in Java, there can be instances where the developer needs to apply a specific functionality upon multiple values. For that, the programmer needs to allocate a separate function and perform the desired functionalities in it. The values are then passed into this function, updated, and returned accordingly. In such instances, passing by reference in Java is of great aid in appending identical functionalities upon the same values.

This blog will elaborate on the approaches to pass by reference in Java.

What is “Pass by Reference” in Java?

Passing by reference in Java means that when a method is called, the method arguments refer to the same variable in memory as the invoker.

How to “Pass by Reference” in Java?

To pass by reference in Java, consider the following approaches:

Example 1: Pass by Reference in Java by Returning an Updated Value

In this example, the initialized integer can be passed as a function argument which is incremented in the function and returned:

int givenNumber = 2;

System.out.println("The given number is: " + givenNumber);

givenNumber = increment(givenNumber);

System.out.println("The updated number is: " + givenNumber);

public static int increment( int updateNum){

updateNum++;

return updateNum;

}

In the above lines of code:

  • Firstly, initialize the integer value and display it.
  • In the next step, invoke the function “increment()” by passing the integer as its argument and displaying the incremented number.
  • Lastly, define a function named “increment()” having the stated parameter that needs to be incremented.
  • In the function definition, increment the passed number and return it.

Output

In this output, it can be seen that the initialized number is passed, incremented, and returned.

Example 2: Pass by Reference in Java by Returning an Incremented Array Element

Now, add the following code:

int givenArray[] = {2,3,4};

System.out.println("The given number is: " + givenArray[1]);

increment(givenArray);

System.out.println("The updated number is: " + givenArray[1]);

}

public static void increment(int increase[]){

increase[1]++;

}

Here:

  • Firstly, declare an array named “givenArray[ ]” containing the integer values.
  • After that, display the indexed array value.
  • Now, pass the array value as a function argument and, likewise, display the incremented integer on the console.
  • Finally, similarly declare the function “increment()” having the parameter pointing to the array value to be incremented.
  • In the function definition, increment the passed value and return it.

Output

In the above outcome, it can be observed that the initialized value is passed and incremented accordingly.

Example 3: Pass by Reference in Java by Updating a Public Member Variable in a Class

In this demonstration, a class variable can be invoked and incremented by referring to the class object:

class customClass {

public int givenNumber;

public customClass() {

givenNumber = 2;

}}

public class Example {

public static void main(String args[]) {

customClass object = new customClass();

System.out.println("The given number is: " + object.givenNumber);

increment(object);

System.out.println("The updated number is: " + object.givenNumber);

}

public static void increment( customClass obj ){

obj.givenNumber++;

}

}

In the above code block:

  • Define a class named “customClass”. Also, specify a public variable within the class.
  • In the next step, create the class constructor “customClass()” and initialize the specified integer within it.
  • After that, create an object of the created class named “object” via the “new” keyword and the “customClass()” constructor, respectively.
  • Now, display the initialized integer by referring to the class object.
  • Invoke the function “increment()” and pass the created object to apply the functionalities of the function upon the class.
  • Finally, declare the function “increment()” and increment the initialized integer by referring to the class object.

Output

The above outcome signifies that the desired requirement has been fulfilled.

Conclusion

To pass by reference in Java, return an updated value, return an incremented array element, or update a public variable in a class. These approaches perform the desired functionality by passing an integer, array element, or class object as a function argument and incrementing the initialized value, respectively. This blog stated the approaches to pass by reference in Java.

Share Button

Source: linuxhint.com

Leave a Reply