Posts

Showing posts from June, 2005

Bits for u

Question: Does Java pass objects by reference or by value? Answer: Since it makes no sense to begin any argument without agreed upon defintions let's formally define our terms. I will use abstract pseudocode to keep the issue from being clouded by the idiom of a particular language. The source of my information is the book "Advanced Programming Language Design" by Raphael A. Finkel. For those unfamiliar with the term below an L-value is an expression that can appear on the left side of an assignment statement. It is basically a way to address where a variable is stored. Variables and other ways to refer to locations in memory are L-values. Most expressions are not L-values, e.g. ( x * 2 ) We assume the presence of a procedure named f that takes a formal parameter s. We call that function giving it an actual parameter g. The calling code: f( g ) The function: procedure f( s ) begin -- body of the procedure end; There are several parameter passin

Conclusion

Parameter passing in Java - by reference or by value? Myth: "Objects are passed by reference, primitives are passed by value" Some proponents of this then say, "Ah, except for immutable objects which are passed by value [etc]" which introduces loads of rules without really tackling how Java works. Fortunately the truth is much simpler: Truth #1: Everything in Java is passed by value. Objects, however, are never passed at all . That needs some explanation - after all, if we can't pass objects, how can we do any work? The answer is that we pass references to objects. That sounds like it's getting dangerously close to the myth, until you look at truth #2: Truth #2: The values of variables are always primitives or references, never objects. This is probably the single most important point in learning Java properly. It's amazing how far you can actually get without knowing it, in fact - but vast numbers of things suddenly make sense when you gr

Pass by value and pass by reference

With the exception of arrays and functions , C always passes arguments `by value': a copy of the value of each argument is passed to the function; the function cannot modify the actual argument passed to it: void foo(int j) { j = 0; /* modifies the copy of the argument received by the function */ } int main(void) { int k=10; foo(k); /* k still equals 10 */ } If you do want a function to modify its argument you can obtain the desired effect using pointer arguments instead: void foo(int *j) { *j = 0; } int main(void) { int k=10; foo(&k); /* k now equals 0 */ } This is sometimes known as `pass by reference' in other languages.

Now the details...

The problem we're facing here is statements like In Java, Objects are passed by reference, and primitives are passed by value. This is half incorrect. Everyone can easily agree that primitives are passed by value; there's no such thing in Java as a pointer/reference to a primitive. However, Objects are not passed by reference . A correct statement would be Object references are passed by value . This may seem like splitting hairs, bit it is far from it. There is a world of difference in meaning. The following examples should help make the distinction. In Java, take the case of public void foo(Dog d) { d = new Dog( "Fifi" ); } Dog aDog = new Dog( "Max" ); foo(aDog); the variable passed in (aDog) is not modified! After calling foo, aDog still points to the "Max" Dog! Many people mistakenly think/state that something like public void foo(Dog d) { d.setName( "Fifi" ); } shows that Java does in fact pass obj

Java is Pass-By-Value ?? is it ?

I finally decided to write up a little something about Java's parameter passing. I'm really tired of hearing folks (incorrectly) state C i also having pass by reference. The terms "pass-by-value" semantics and "pass-by-reference" semantics have very precise definitions, and they're often horribly abused when folks talk about Java in that case C , C++. I want to correct that... The following is how I'd describe these Pass-by-value The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could choose parameter storage differently. Pass-by-reference The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal