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 parameter , it is actually using the actual parameter.
Java is strictly pass-by-value, exactly as in C. Read the Java Language Specification (JLS).
It's spelled out, and it's correct. (See http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#37472)
Conclusion : : Java has pointers and is strictly pass-by-value. There's no funky rules. It's simple, clean, and clear. (Well, as clear as the evil C++-like syntax will allow ;)
Comments