This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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.
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?
Joined: May 28, 2008
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 ]
Joined: Oct 13, 2005
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."
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.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Jul 11, 2001
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).