aspose file tools*
The moose likes Beginning Java and the fly likes A greenhorn's question concerning method parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A greenhorn Watch "A greenhorn New topic
Author

A greenhorn's question concerning method parameters

Kora Darmstaedter
Greenhorn

Joined: May 28, 2008
Posts: 12
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

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

if what the reference points to can be changed, via a method, for example, then yes.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: May 28, 2008
Posts: 12
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

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Jul 11, 2001
Posts: 14112
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A greenhorn's question concerning method parameters
 
Similar Threads
Method parameter
Pass By Reference
Simple test tool for functions
a confused concept
Logging and reflection/introspection