in Java passing StringBuilders to methods seems it's passing by Reference but in C# it seems it's passing by value, is it like this? do you think it may work only with ref keyword? what're the differences overall?
-
1This is not true, in both platforms the StringBuilder is bassed by refsternr– sternr2011-08-14 14:00:15 +00:00Commented Aug 14, 2011 at 14:00
-
There is no ref keyword in Java like that in C#, isn't there ? I think passing by value is the default in java.Aussay Marshal– Aussay Marshal2011-08-14 19:19:03 +00:00Commented Aug 14, 2011 at 19:19
Add a comment
|
1 Answer
A StringBuilder is an object, so whether you pass the reference to it by reference or by value, doesn't matter at all. Java only has pass-by-value. If you pass the reference to it by reference, you are able to change the original reference from the method, that't the only difference. The object you reference is always changed whether you send the reference to it by value or by reference.
Why do you think there is a difference? Do you have any example code that seems to indicate there is a difference?
2 Comments
Vladimir
By default, parameters in C# are passed by value. All of them, even objects.
Erik A. Brandstadmoen
Vladimir, actually the objects are not passed around at all. Only references to the objects. References to objects are sometimes called pointers, however, this is easily confused with C-style pointers, so reference is a more connotation-neutral word. What happens when you "pass an object" (actually pass a reference to an object) to a method in C# (and other OO languages), is that a new reference is created, which points to the same object as the object the original reference pointed to. So, pedantically speaking, the object is never passed at all... :)