Re: pointer c++//C#
* Adrian Petrescu:
On Thu, 03 Dec 2009 12:28:20 -0800, Joshua Maurice wrote:
This a very anal, but important type. Sometimes I see silly discussions
about Java when people write bad code, believing that Java has pass by
reference semantics, when it does not. It has pass by value semantics,
except that most values are "references", aka pointers. The same is true
for C#.
What you say is only true for primitive types in Java (int, double, char,
etc) and Strings. As soon as you start passing Objects around (i.e,
almost all the time in "real life"), you'll see the "pass by reference"
behavior. You have to go to some lengths (Cloneable interface) to make it
work the way you describe.
Sorry, that's incorrect.
Java passes everything by value. There is no type-dependent difference, even
though it might seem so when one is not familiar with what goes on under the
hood. There is no way in Java to have the call
foo( v );
change the value of the variable v, although the object that v references can be
changed.
Thus, experienced Java programmers sometimes simulate pass by reference by
introducing an additional indirection (the universal solution to anything), such
as passing an array -- since the array object can be modified...
On the other hand, C# has pass by reference. C# inherited almost all the Java
semantics but differs in this key respect. So since C# is otherwise so similar
to Java it is perhaps easier to see that Java lacks pass by reference when
comparing it to C#, which does have pass by reference (instead of comparing Java
to the very dissimilar C++, where you have to understand things to compare).
Cheers & hth.,
- Alf