Java Out Parameter

Sheeraz Gul Oct 12, 2023
  1. the Out Parameter in Java
  2. Pass Object References in Java
  3. Conclusion
Java Out Parameter

In programming languages like Java and C#, how method parameters are handled can be quite distinctive.

In Java, parameters are passed by value, even when dealing with object references, which sets it apart from C#. In C#, you have the convenience of keywords like out and ref for passing parameters by reference, facilitating multiple value returns from methods.

However, Java takes a different approach, lacking these keywords. To achieve similar outcomes in Java, you’ll learn how to encapsulate parameters within objects and pass references to these objects as method arguments.

This tutorial sheds light on this technique, offering insights into how to manipulate variables indirectly within Java methods and extend the impact of these changes beyond their immediate scope.

the Out Parameter in Java

In C#, the out parameter allows a method to return multiple values. However, Java does not support this feature natively.

As mentioned above, in Java, parameters are always passed by value, meaning the values of the variables are passed to the method, not the actual variables themselves.

Suppose we have the following C# code example with the out parameter:

using System;

class Out_Parameter {
  static void Divide(int x, int y, out int divide_result, out int divide_remainder) {
    divide_result = x / y;
    divide_remainder = x % y;
  }

  static void Main() {
    for (int x = 1; x < 5; x++)
      for (int y = 1; y < 5; y++) {
        int result, remainder;
        Divide(x, y, out result, out remainder);
        Console.WriteLine("{0} / {1} = {2}r{3}", x, y, result, remainder);
      }
  }
}

Output:

1 / 1 = 1r0
1 / 2 = 0r1
1 / 3 = 0r1
1 / 4 = 0r1
2 / 1 = 2r0
2 / 2 = 1r0
2 / 3 = 0r2
2 / 4 = 0r2
3 / 1 = 3r0
3 / 2 = 1r1
3 / 3 = 1r0
3 / 4 = 0r3
4 / 1 = 4r0
4 / 2 = 2r0
4 / 3 = 1r1
4 / 4 = 1r0

To achieve similar functionality in Java, we can use the approach of wrapping the parameters inside an object or using an array to hold the values we want to modify and return.

Let’s have an example to see how this can be done:

First, we create a simple wrapper class that holds the values we want to modify. In this case, we’ll create a Result class to hold the division result and remainder.

class Result {
  int result;
  int remainder;

  public Result(int result, int remainder) {
    this.result = result;
    this.remainder = remainder;
  }
}

Next, we modify the divide method to accept an instance of the Result class instead of primitive int variables.

static void divide(int x, int y, Result result) {
  result.result = x / y;
  result.remainder = x % y;
  System.out.println(x + "/" + y + " = " + result.result + " r " + result.remainder);
}

In the main method, we create an instance of the Result class, pass it to the divide method, and then retrieve the division result and remainder from the Result instance.

public static void main(String[] args) {
  for (int x = 1; x < 5; x++) {
    for (int y = 1; y < 5; y++) {
      Result result = new Result(0, 0);
      divide(x, y, result);
    }
  }
}

By following these steps and utilizing the Result class as a wrapper to hold the result and remainder values, we achieve a similar functionality to the out parameter in C# in Java.

Complete example code:

package delftstack;

public class Out_Parameter {
  static void divide(int x, int y, int divide_result, int divide_remainder) {
    divide_result = x / y;
    divide_remainder = x % y;
    System.out.println(x + "/" + y + " = " + divide_result + " r " + divide_remainder);
  }

  public static void main(String[] args) {
    for (int x = 1; x < 5; x++)
      for (int y = 1; y < 5; y++) {
        int result = 0, remainder = 0;
        divide(x, y, result, remainder);
      }
  }
}

In this Java example, a method named divide takes parameters for division (x and y) as well as two additional parameters (divide_result and divide_remainder), which would act as the equivalent of out parameters. Inside the method, these parameters are modified to store the results of the division and remainder calculations.

Output:

1/1 = 1 r 0
1/2 = 0 r 1
1/3 = 0 r 1
1/4 = 0 r 1
2/1 = 2 r 0
2/2 = 1 r 0
2/3 = 0 r 2
2/4 = 0 r 2
3/1 = 3 r 0
3/2 = 1 r 1
3/3 = 1 r 0
3/4 = 0 r 3
4/1 = 4 r 0
4/2 = 2 r 0
4/3 = 1 r 1
4/4 = 1 r 0

Pass Object References in Java

In Java, when you pass an object to a method, you’re actually passing a copy of the reference to the object.

This means that modifications to the object’s state (its properties or fields) made within the method will affect the original object. This is because both the original reference and the copy of the reference point to the same object in memory.

Here’s a more detailed explanation of passing objects in Java:

public class Person {
  private String name;

  public Person(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

public class Main {
  public static void changeName(Person person) {
    person.setName("New Name");
  }

  public static void main(String[] args) {
    Person person = new Person("John");
    System.out.println("Before change: " + person.getName());

    changeName(person);
    System.out.println("After change: " + person.getName());
  }
}

In this example, we have a Person class with a name property. The changeName method takes a Person object and modifies its name.

When the changeName method is called and modifies the object’s name, it affects the original Person object because both the original reference and the copy of the reference point to the same object.

It’s important to note that if the object itself is mutable (its fields can be changed), modifications made within the method will be visible outside the method.

If the object is immutable (its fields cannot be changed), any attempts to modify the object will result in a new object being created.

Conclusion

Although Java doesn’t have direct support for out parameters like C#, we can achieve similar functionality by passing objects or arrays to methods, enabling the modification of values and mimicking the behavior of out parameters.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Parameter