Hi,
I am a beginner in Java & i have some questions to ask you guys & learn things by communicating my questions with u?
My question is,
What is difference between pass by value & pass by reference in Java?
Hi,
I am a beginner in Java & i have some questions to ask you guys & learn things by communicating my questions with u?
My question is,
What is difference between pass by value & pass by reference in Java?
Java is strictly pass-by-value, exactly as in C. Read the JLS. It's spelled out, and it's correct. (See http://java.sun.com/docs/books/jls/s...doc.html#37472)
In short: 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
In compiler terms, "pass by reference" means if you pass a variable into a method, its value can be modified. This is possible in many languages, like Pascal, Ada, and C++, but not in many other languages like C and Java. (Pascal had "var", Ada had "out", C++ had "&")
For Java, take the case of
the variable passed in is not modified! After calling foo, aDog still points to the "Max" Dog!Code:public void foo(Dog d) { d = new Dog("Fifi"); } Dog aDog = new Dog("Max"); foo(aDog);
Many people mistakenly think/state that something like
shows that Java does in fact pass objects by reference.Code:public void foo(Dog d) { d.setName("Fifi"); }
The mistake they make is in the definition of
itself. When you writeCode:Dog d;
you are defining a pointer to a Dog object, not a Dog object itself.Code:Dog d;
Calling
passes the value of d to foo. The value of a pointer is similar to a memory address.Code:foo(d);
The use of the word "reference" in Java was an incredibly poor choice. Java has pointers, plain and simple. The designers of Java wanted to try to make a distinction between C/C++ pointers and Java pointers, so they picked another term. Under the covers, pointers are implemented very differently in Java and C/C++, and Java protects the pointer values, disallowing operations such as pointer arithmetic and invalid runtime casting.
However, it makes no difference how pointers are implemented under the covers. You program with them exactly the same way in Java as you would in C or C++. The syntax is just slightly different.
In Java,
is exactly like C or C++'sCode:Dog d; // Java
And usingCode:Dog *d; // C++
is exactly like C++'sCode:d.setName("Fifi"); // Java
To sum up: Java has pointers, and the value of the pointer is passed in. There's no way to actually pass an object itself as a parameter. You can only pass a pointer to an object.Code:d->setName("Fifi"); // C++
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 (for reading or writing), it is actually using the actual parameter.
http://javadude.com/articles/passbyvalue.htm
Not sure how familiar are you with C. But, people who know C or C++ can understand this in this way.
If we are passing the value to the sub-routine, then it is called pass-by-value. The passed value is stored in another variable in called functions and operations are performed on that new variable. So, any changes made to the value (or variable) are not reflected back.
If we are passing reference (pointer) rather value of the object, then it is called pass-by-reference. In this case, we are passing the reference to the object and all changes we make to the value it is holding will be reflected back automatically in program.
In Pass by value copy of that variable sended to local variables of the function.
In call by ref reference of that variable is sended.
one more diff is in call by value the changes made to formal parameters will not affect actual parameters.
in call by ref changes made to formal will affected by actual params
in Java call by value is achieved by normal parameter passing and call by ref is achieved using passing of objects as parameters to functions.
Pass by Reference means the passing the address itself rather than passing the value and pass by value means passing a copy of the value as an argument.
This is simple enough, however there is an important but simple principle at work here. If a variable is passed, the method receives a copy of the variable's value. The value of the original variable cannot be changed within the method. This seems reasonable because the method only has a copy of the value; it does not have access to the original variable. This process is called pass by value.
However, if the variable passed is an object, then the effect is different. We often say things like, "this method returns an object ...", or "this method is passed an object as an argument ..." But this is not quite true, more precisely, we should say, something like "this method returns a reference to an object ..." or "this method is passed a reference to an object as an argument ..."
Please have a look for further information about the differences
http://www.javacertificate.net/passbyvalue.htm
I hope this helps you!
Bookmarks