A simple question about parameters to functions

hello,

Passing parameters between functions in javasuppose to be "by reference".

I have a simple test case that it doesn't work.

I'll be happy if someone will tell me what is wrong:

public class Test

{

String text ;

public static void main(String[] args)

{

Test test = new Test();

test.poo() ;

}

public void poo ()

{

text=new String(" bbb") ;

foo (text) ;

System.out.println ("final text= " +text) ;

}

private void foo(String text)

{

System.out.println ("text before update= " +text) ;

text="aaa" ;

System.out.println ("text after update= " +text) ;

}

}

The output here is:

text before update= bbb

text after update= aaa

final text= bbb

How can you explain that?

thank you,

Moran

[895 byte] By [rbarma] at [2007-10-2 13:46:02]
# 1
Java passes references by value, and does not pass objects by reference.The forum is already full of endless discussions about that.- [url= http://www.yoda.arachsys.com/java/passing.html]Parameter passing in Java[/url] -
TimTheEnchantora at 2007-7-13 11:43:21 > top of Java-index,Java Essentials,Java Programming...
# 2

Java always use value! This is problematic in your code because the scope of a variabel is confused. When you pass a value of an Object to a method you also create a new object local to that method. It is done in your code in the sentence

private void foo(String text)

You could have called it any name. It still would have been a new object. Since you have two objects named text java will update the local one. If you rather want to update the variabel from the class you must use the form:

this.text="bbb"

Therefore do not pass objects to a method that you intend to update without returning the object. But preferred method is to access the variables directly with the this reference.

ostensea at 2007-7-13 11:43:21 > top of Java-index,Java Essentials,Java Programming...
# 3
And your "simple test case" wouldn't even work in a pass-by-reference environment, since you're moving the reference (text = "...";) instead of changing the object it's pointing to.
CeciNEstPasUnProgrammeura at 2007-7-13 11:43:21 > top of Java-index,Java Essentials,Java Programming...
# 4

> Passing parameters between functions in java suppose to be "by reference".

Nope. First, we call them "methods" rather than "functions". Second, everything in Java is passed "by value". Everything.

Pass-by-value

- When an argument is passed to a function, the invoked function gets a copy of the original value.

- The local variable inside the method declaration is not connected to the caller's argument; any changes made to the values of the local variables inside the body of the method will have no effect on the values of the arguments in the method call.

- If the copied value in the local variable happens to be a reference (or "pointer") to an object, the variable can be used to modify the object to which the reference points.

Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory.... The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

-- James Gosling, et al., The Java Programming Language, 4th Edition

yawmarka at 2007-7-13 11:43:21 > top of Java-index,Java Essentials,Java Programming...