views:

801

answers:

6

Hello, Just a quick question about how parameters are passed in Java...

...
            if ((index = stdout.indexOf(pattern)) != -1) {
                tidy(stdout, index + pattern.length());
                return true;
            } else if ((index = stderr.indexOf(pattern)) != -1) {
                tidy(stderr, index + pattern.length());
                return true;
...

    private void tidy(StringBuffer buffer, int i) {
        logger.info("Truncating buffer: " + buffer);
        buffer = new StringBuffer(buffer.substring(i));
        logger.info("Buffer now: " + buffer);
    }

In this case, will stdout and stderr (used as parameters in tidy()) have their values changed to new StringBuffer(buffer.substring(i))? My assumption is that they will as object variables(object pointers) are always passed-by-value?

+12  A: 

You misstate what is going on here -- the object references are passed by value (a copy of the reference is made), so stdout and stderr do not get modified when you call tidy. The copies that are made of them get modified when you execute line 2 of tidy.

Parameter passing in Java is a source of confusion for a lot of people. Here's a good explanation.

Nick Meyer
thanks! i like your reference to this 'pass-by-value' problem
i would not call that a good explanation... i don't understand it.
Victor
+1  A: 

No, stdout and stderr will not be changed, and yes, parameters are passed by value.

The variable "buffer" will be set equal to stdout, meaning buffer will point to the same object at first. When buffer is changed to point to a new object, the old stdout reference will still point to the old object.

UncleO
A: 

Your assumption is incorrect. Why?

Because function parameters are always passed-by-value.

When you do:

buffer = new StringBuffer(buffer.substring(i));

you are assigning a new StringBuffer to a local reference that was copied from the parameter reference. So, the original buffer doesn't change.

bruno conde
+2  A: 

Java parameters are always pass-by-value, period.

Here's a cute article explaining the phenomenon: http://www.javaranch.com/campfire/StoryPassBy.jsp

In your case, it doesn't matter if it's pbr or pbv, because you are re-assigning sb to a new object (by calling buffer = new StringBuffer(...) and buffer.subString(...) does not mutate the object itself).

Cambium
A: 

Hello, my intention in asking this question in the first place was a 'quick-check,' as I distinctively remember reading(Core Java, http://www.horstmann.com/corejava.html) that java function parameters are always passed by value regardless of its object type(whether if is primitive or object datatype)...

yes, this is correct.
Cambium
You were correct about the pass-by-value part, but what you said about stdout and stderr being modified when buffer was reassigned was incorrect.
Nick Meyer
+1  A: 

From James Gosling in "The Java Programming Language":

"...There is exactly one parameter passing mode in Java - pass by value - and that keeps things simple. .."

I think that's the final authority on this.

duffymo
+1 for passing irony by value
James M.