return to function parameter

C

column

Hello,

Is it possible return value to function parameter in JAVA?


public static boolean a(String s)
{
s= new String ("aaa");
return true;
}

public static void main(String[] args)
{
String s = new String(String "bbb");
boolean d= a(s);

System.out.print(s);

}

It prints bbb, but I need aaa. What is solution?
 
A

Andreas Leitgeb

column said:
Hello,
Is it possible return value to function parameter in JAVA?

Only by using a mutable wrapper:

public static boolean a(String[] w)
{
w[0]= new String ("aaa");
return true;
}

public static void main(String[] args)
{
String s = "bbb";
String[] w={s}; // the mutable wrapper: an array
boolean d= a(w);

System.out.print(w[0]);
// System.out.print(s); // would still print "bbb"
}
It prints bbb, but I need aaa. What is solution?
This modified prog should print "aaa".

This is of course not the only way to get it done,
but assigning to the parameter directly is *never*
visible outside.
If in my method "a" above you assigned to "w", rather
than to "w[0]", then this would again be invisible
outside.
 
A

Andrea Francia

In Java all the argument are passed by value.
Also the reference to an Object are passed by value.

You can bypass this behavior passing an array which contains a reference
to a String.

public static boolean a(String[] s)
{
s[0] = new String ("aaa");
return true;
}

public static void main(String[] args)
{
String[] s = new String[1];
s[0] = new String(String "bbb");

boolean d= a(s);

System.out.print(s[0]);
}

Hello,

Is it possible return value to function parameter in JAVA?


public static boolean a(String s)
{
s= new String ("aaa");
return true;
}

public static void main(String[] args)
{
String s = new String(String "bbb");
boolean d= a(s);

System.out.print(s);

}

It prints bbb, but I need aaa. What is solution?
 
J

Jan Thomä

Andreas said:
column said:
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
 
A

Andreas Leitgeb

Jan Thomä said:
Andreas said:
column said:
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.
 
P

Patricia Shanahan

column said:
Hello,

Is it possible return value to function parameter in JAVA?


public static boolean a(String s)
{
s= new String ("aaa");
return true;
}

public static void main(String[] args)
{
String s = new String(String "bbb");
boolean d= a(s);

System.out.print(s);

}

It prints bbb, but I need aaa. What is solution?

In addition to the suggestions for returning the parameter, consider
whether this asymmetric treatment of results makes sense.

1. If the boolean indicates success or failure, consider making the
method return a String and throw an exception if it fails:

public static String a(String s){
if(some problem condition){
throw new SomeException();
}else{
return "aaa";
}
}

2. If the boolean and the String are both results, consider returning an
object that encapsulates both of them:

public class aResults{
public final String s;
public final boolean b;
public aResults(String s; boolean b){
this.s = s;
this.a = a;
}
}

public static aResults a(String s){
return new aResults("aaa",true);
}

Of course, the aResults class could make the fields private and use
getters to return their values.

Patricia
 
R

Roedy Green

public static boolean a(String s)
{
s= new String ("aaa");
return true;
}

public static void main(String[] args)
{
String s = new String(String "bbb");
boolean d= a(s);

System.out.print(s);

}

a method can't make the caller's variables point to different objects.
See http://mindprod.com/jgloss/callbyvalue.html

All you can do is return a new object and have the caller make the
change.

E.g.
/**
* Remove any spaces from the beginning of the string
* Does not modify the original String.
* @param s string to trim.
* @return string with left spaces trimmed off.
*/
String leftTrim ( String s )
{
for ( int i=0; i< s.length; i++ )
{
if ( s.charAt( i ) != 0 )
{
return s.substring( i );
}
}
return "";
}

String s = " hello";
String t = leftTrim( s );
 
R

Roedy Green

1. If the boolean indicates success or failure, consider making the
method return a String and throw an exception if it fails:

public static String a(String s){
if(some problem condition){
throw new SomeException();
}else{
return "aaa";
}
}

2. If the boolean and the String are both results, consider returning an
object that encapsulates both of them:

public class aResults{
public final String s;
public final boolean b;
public aResults(String s; boolean b){
this.s = s;
this.a = a;
}

other possibility is to return the string, but return null if there is
a problem.
 
C

column

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

Holder is nice. Unfortunately I must use Java 1.4
 
A

Andreas Leitgeb

column said:
Holder is nice. Unfortunately I must use Java 1.4

Then the String-array seems to be better-suited.

Or you can also make a special StringHolder:
public class StringHolder {
public String value;
}
and ditto for each other class that you want to
"pass by ref", if you prefer this approach.
 
L

Lew

Andreas said:
Then the String-array seems to be better-suited.

Or you can also make a special StringHolder:
public class StringHolder {
public String value;
}
and ditto for each other class that you want to
"pass by ref", if you prefer this approach.

Or Object holder:

public class Holder
{
public Object value;
}

Then you cast the value when you retrieve it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,319
Latest member
LorenFlann

Latest Threads

Top