Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output Parameters in Java

With a third party API I observed the following.

Instead of using,

public static string getString(){
   return "Hello World";
}

it uses something like

public static void getString(String output){

}

and I am getting the "output" string assigned.

I am curious about the reason of implementing such functionality. What are the advantages of using such output parameters?

like image 380
Chathuranga Chandrasekara Avatar asked Sep 10 '09 07:09

Chathuranga Chandrasekara


People also ask

What are output parameters Java?

Output parameters. A method may occasionally need to use a parameter for its return value - what might be loosely called an "output parameter" or a "result parameter". The caller creates an output parameter object, and then passes it to a method which changes the state of the object (its data).

What is an output parameter?

Output parameters are the parameters that are fetched from the response of a service call. These are formatted according to the attributes you configure for the output before displaying on the device. The service parameters have a scope and data type attached to them.

Does Java support output parameters?

Java does not support output parameters. You can use a return value, or pass in an object as a parameter and modify the object.

What are the parameters of Java?

Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.


2 Answers

Something isn't right in your example.

class Foo {

    public static void main(String[] args) {
        String x = "foo";
        getString(x);
        System.out.println(x);
    }

    public static void getString(String output){
        output = "Hello World"
    }
}

In the above program, the string "foo" will be output, not "Hello World".

Some types are mutable, in which case you can modify an object passed into a function. For immutable types (such as String), you would have to build some sort of wrapper class that you can pass around instead:

class Holder<T> {
    public Holder(T value) {
        this.value = value;
    }
    public T value;
}

Then you can instead pass around the holder:

public static void main(String[] args) {
    String x = "foo";
    Holder<String> h = new Holder(x);
    getString(h);
    System.out.println(h.value);
}

public static void getString(Holder<String> output){
    output.value = "Hello World"
}
like image 60
Adam Batkin Avatar answered Oct 01 '22 09:10

Adam Batkin


That example is wrong, Java does not have output parameters.

One thing you could do to emulate this behaviour is:

public void doSomething(String[] output) {
    output[0] = "Hello World!";
}

But IMHO this sucks on multiple levels. :)

If you want a method to return something, make it return it. If you need to return multiple objects, create a container class to put these objects into and return that.

like image 37
Bombe Avatar answered Oct 01 '22 09:10

Bombe