Hi,
I actually thought that I had a good idea of how passing values in Java actually work, since that was part of the SCJP cert which I have passed. That was until today, when I at work discovered a method like this:
public void toCommand(Stringbuffer buf) {
buf.append("blablabla");
}
Then the caller of that method used the function like this:
StringBuffer buf = new StringBuffer();
toCommand(buf);
String str = buf.toString();
Now I thought that that code would give str the value "", but it actually give it the value from the mehod. How is this possible? I thought things didnt work like that in Java?
Either way... it should be considered a bad practice to write code like this in Java, right? Because I can imagine it can bring some confusion with it.
I actually spent some time searching on this, but my interpretation of what these sources are saying, is that it shouldnt work. What am I missing?
http://www.yoda.arachsys.com/java/passing.html
http://javadude.com/articles/passbyvalue.htm
Sebastian