Re: return to function parameter
Jan Thom? <kork@insomnia-hq.de> wrote:
Andreas Leitgeb wrote:
column <column.column@gmail.com> wrote:
Is it possible return value to function parameter in JAVA?
Only by using a mutable wrapper:
public static boolean a(String[] w)
Using a String[] is bound to cause problems
I disagree. It's no more dangerous than passing a null
in the original example. If you pass garbage, then it
doesn't really matter, if it results in a NullPointerExcepton
or an ArrayIndexOutOfBoundsException, or some specific
StringIsNotFooableException, does it? Were we on C++ here,
this would of course be a different issue :-)
public class Holder<T> {
public T value;
}
and then
public static boolean a(Holder<String> value) {
value.value = "aaa";
return true;
}
The holder, of course still can be null, too, which is quite
the same type of problem as a null or zero-length array.
You can use this holder for any kind of object (not just strings) and you
cannot use it the wrong way like you could do with an array.
The array also works for ... ok most types. It doesn't work with
generics, so if you really need those for return-parameter, then
use the Holder, otherwise the array is less fuss (no need to declare
a Holder class).
Btw. do not use new String("foo") , this is redundant at best and a
performance hit at worst, just type "foo" and let the compiler do the rest
for you :)
I have already eliminated one instance of 'new String("...")'. If I've
missed another, or if your comment was to the OP, (or both) then of course
you're right with that.