Re: return to function parameter
Andreas Leitgeb wrote:
column <column.column@gmail.com> wrote:
Hello,
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 because you can give in any kind
of String[] (even a String[0], which breaks your implementation with a nice
ArrayIndexOutOfBoundsException). A more generic solution would be a
typed "Holder" object like this:
public class Holder<T> {
public T value;
}
and then
public static boolean a(Holder<String> value) {
value.value = "aaa";
return true;
}
public static void main(..) {
Holder<String> holder = new Holder<String>();
holder.value = "bbb";
boolean d = a(holder);
System.out.println(holder.value);
}
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.
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 :)
Best regards,
Jan
--
_________________________________________________________________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de