• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A greenhorn's question concerning method parameters

 
Kora Darmstaedter
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question concerning method parameters.
Here's a code example:

public boolean getTwoStrings (String a, String b) {

a = "Hello";
b = "World";
return true;

}

Until earlier that day I thought that once the method getTwoStrings has finished, a would be "Hello" and b would be "World". Much to my surprise, this is not the case.

Well, I have done some reading and figured out, why: "Java is pass-by-value. That means pass-by-copy" (Head First Java, p. 77).

What I'm wondering now is, whether there is any possibility at all, to return something through method parameters. Or can Java methods only return something through the return value?

(I'm much used to using parmeters for output as well as input from other languages like C++ or Oracle PL/SQL so it would be so handy if it worked in Java, too!)

Thanks,
Kora
[ May 29, 2008: Message edited by: Kora Darmstaedter ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't believe that java supports call-by-reference, which in c and c++ is like:



You can't call by reference in java. I guess you could say the only things java can't do that c++ and c can do are (multiple inheritance, operator overloading and pass-by-reference).

which personally, I don't see the need for pass-by-reference, you could easily do:




Justin Fox
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if what the reference points to can be changed, via a method, for example, then yes.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A pretty handy and generic way of doing what Fred suggested wound be using arrays as parameters and changing the entries:


Since there is no way of specifying an array type parameter has to have a specific length, however, this way of doing "call by reference" should be used in private code only, i.e. in methods you have control over regarding with what actual parameter they are invoked. Or you have to write a pretty clear JavaDoc comment that the parameter array is expected to have a specific length.
To circumvent the length problem, you could use a collection and generics, but then zou have little control regarding what's aalready in the collection you get passed as a parameter.
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Justin, I am not sure that C supports pass-by-reference, but it uses pointers as output parameters to "mimic" pass-by-reference.

Is your C code correct? Shouldn't it read?

There you are not passing a reference; you are passing a number which is obtained from c with the & operator and that number represents the location in memory of something.

Anybody who is more up-to-date with C than me?
 
Kora Darmstaedter
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again and thanks a lot for your suggestions which have been very helpful to understand the concept and implications of �pass by value�. Meanwhile (with a little help of others �) I have found another solution: I do no longer pass Strings but StringBuffers to which I append the Strings I want to return. It works fine � or does anyone of you have objections to this?

Kora
[ May 29, 2008: Message edited by: Kora Darmstaedter ]
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you pass the value of a reference type, including a StringBuffer, you are implicitly permitting the recipient method to use any public methods of that reference type.
So passing a StringBuilder implicitly permits use of the append() method.

This is not altering the reference; it is exactly the same StringBuilder object before and after. It is however possible to manipulate the object and alter its state under "pass-by-value."
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Actually, Justin, I am not sure that C supports pass-by-reference, but it uses pointers as output parameters to "mimic" pass-by-reference.


A short google search actually seems to suggest that pass-by-reference was introduced with C++, and not available in C.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pantoffel-Elch:
Until earlier that day I thought that once the method getTwoStrings has finished, a would be "Hello" and b would be "World". Much to my surprise, this is not the case.


It's true that it is not the case - but for a slightly different reason than you said.

a is a *reference* to a String. "Hello" is a String. Therefore, a can *never* be "Hello", it can only reference a String "Hello".

And in fact, the reference a references a String "Hello" at the end of the method. But that reference doesn't even exist outside of the method (parameters are basically local variables in that regard).

I hope that makes it a bit more clear...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic