Passing primitive types as out param in Java

I need to return / update a boolean by returning a list of material from a method. Java cannot return tuples, and I did not want to create a separate class for this, so I decided to pass bool as an out parameter. What our C ++ client does is pass the bool by reference. This will work for a normal class, since the java type has a pass-by-ref for objects (see Is Java a "pass-by-reference" or "pass-by-value" "? For a good discussion of this). But the classes Wrappers such as Boolean keep their primitive value unchanged, so it cannot be updated this way.

Using a boolean array (with a single entry) seems hokey, but perhaps this is the easiest thing that works. Alternatively, you can return a boolean value and pass the created list back as an out parameter, not as a return parameter [but then the java client deviates from C ++, and it is best if they follow basically the same approach - FYI also needed in C #. ]

+6
java pass-by-reference parameters
source share
6 answers

As you say, the Boolen wrapper class is immutable, so you cannot use it to get the output variable.

Going to a new list to get the result of the list and return a boolean will be most natural for me, but you have a good reason; keeping the interface about the same for multiple implementations.

You can solve this problem by creating your own mutable Boolean and passing a link to it, for example:

public class Example { public class BooleanResult { private boolean result; public void setResult(boolean value) { result = value; } public boolean getResult() { return result; } } // .... public List doSomething(BooleanResult out) { List list; boolean result; // ... out.setResult(result); return list; } } 

and use it like:

 Example.BooleanResult out = new Example.BooleanResult(); List list = example.doSomething(out); boolean result = out.getResult(); 
+7
source share

No, Java does not have end-to-end references for objects; it has "pass the link by value", which is not the same.

You definitely cannot pass anything as an "out" parameter or by reference in Java - although you could in C #.

Could you encapsulate the "Boolean plus list" in another type? Are they really related? Using a logical array is definitely pretty ugly. You can write a shell type of MutableBoolean - but then again, it's pretty ugly. Normally, I would say that returning two things suggests that the method should be split, but when one of them is logical, it can be quite reasonable - for example, int.TryParse , etc. From C #.

+14
source share

I need to return / update a boolean by returning a list of material from a method. Java cannot return tuples, and I did not want to create a separate class for this

I ran into the same problem and the best IMHO solution is usually:

Just forget about your worries and make a separate class.

I used to hesitate in this matter, but classes are designed to encapsulate values, so go ahead and do it.

Most likely, as soon as your function returns an instance of a custom class, you will find that there are additional functions that fit well into this class or other methods that can use the class as a parameter or return value, and soon the class will be quite useful :-) .

If you really do not want to do this, you can always put everything in java.util.List or java.util.Map and return it. But this is really ugly :-( I did it too and regret it. At first it may seem simple, but as the code develops and grows, readability suffers (was that List of Integer, then Double or vice versa?) Classes are much more useful.

Note. If you think that a regular top-level class is redundant, you can use a nested class that is good for classes for "local use".

+4
source share

What I did to return multiple values ​​is to change the return type to return an array of objects. [0] may be a bool and [1] may be a list.

The disadvantage is that the caller must know in order to return the returned objects appropriately from the array of objects, and the compiler cannot check you.

Edit: Here is an SO response to return a pair of values ​​from Java.

+1
source share

You cannot use any of the primitive types as the "out" parameter in Java. It just won't happen anytime.

You also cannot use any of the object versions, such as Boolean, Integer, Float, Double, etc., as the "out" parameters, since they are immutable. What you can do is either write your own class, or use something like MutableBoolean in commons-lang ( http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/ commons / lang3 / mutable / MutableBoolean.html ).

For example:

 public void setTrue(MutableBoolean b) { b.setValue(Boolean.TRUE); } 
+1
source share

As mentioned in rsp and many others, it is immutable. However, there is a logical variation of AtomicBoolean that was introduced as part of java.util.concurrent.atomic in Java version 1.5. If you don't want to create classes to handle such trivial problems, AtomicBoolean is the way to go. Check this. Run the following code to see for yourself.

 public static void main(String[] args) { AtomicBoolean value = new AtomicBoolean(false); System.out.println(value); // Prints false someMethod(value); System.out.println(value); // Prints true } void someMethod(AtomicBoolean a) { a.set(true); } 
-one
source share

All Articles