Pass by value or Pass by reference in Java?
-
I read many articles and all says Java is pass by value. But i still don't construe the difference between pass by value and reference. I wrote a sample program and it executes like this.. public class PassByValue { private int a; private int b; public PassByValue(int a, int b) { this.a = a; this.b = b; } public static int mul(int a, int b) { int z = a * b; System.out.println("The Value of Z inside mul: " + z); return z; } public static void main(String args[]) { int z = 100; int x = 40; int y = 20; mul(x,y); PassByValue pass = new PassByValue(x,y); mul(pass.a,pass.b); System.out.println("The Value of Z" + z); } } Execution 800 800 and 100 Can anyone explain me these questions... What is Pass By Value means... Answer: Its just passing the numbers or value stored in the variable to a function. Am i right or wrong. How do you say Java is Pass By Value? Why is Java is Pass By Value and not by reference? Does the above program Tries shows an example of Pass by value and Reference... but still does things via Pass by Value only... I wrote that program.
-
Answer:
You are right in your answer, but you are lacking detail. If you do Dog d = new Dog() d is a reference to an instance of Dog, i.e. What pass by value means is that you when pass d into a method walkDog(d); the a copy of the reference to the Dog is passed into the method. So you have 2 references to the one instance of the Dog, the one in the calling scope, and the one in the called scope. Lets say in the walkDog method there is a line d = new Dog(); the d reference in the method only points to the new Dog. The original reference where the method was first called still points to the original Dog. If Java had pass by reference, the same reference, not a copy of the reference would be used in the method, and so changing the value of the reference would affect the reference in both the calling and the called scope. EDIT -- based on your comment, I want to make on thing clear. Since the reference in the original scope and the method scope both point to the same object, you can still change things on that object in both scopes. So if you do d.drinkWater(); in the walkDog method, and drinkWater changes some variable on the dog object, then since both references point to the same object, which was changed. It's only a distinction between what the references actually are in each scope. But it does come up a lot.
theJava at Stack Overflow Visit the source
Other answers
The confusion is probably due to the fact that a variable can't contain an object in the first place. A variable can only contain a reference to an object. (In other words, objects aren't passed at all, not by reference, not by value.) Once you realize this, it is quite clear that nothing is pass-by-reference in Java. A variable refering to an object stores a reference, and this reference is passed by value. 1. What is Pass By Value means... Answer: Its just passing the numbers or value stored in the variable to a function. Am i right or wrong. That's right. The value contained in the variable is passed, and not the variable itself. 1. How do you say Java is Pass By Value? Java is pass by value because primitives are passed by value, and references are passed by value. (Objects are never passed.) You can't implement a swap method in Java for instance. I.e., you can't do String str1 = "hello"; String str2 = "world"; swap(str1, str2); // str1 can't refer to anything but "hello" // str2 can't refer to anything but "world" 2. Why is Java is Pass By Value and not by reference? As explained above, even references are passed by value.
aioobe
You can think of pass-by-value as passing only the value contained in the variable and not the variable itself. So then the value is copied into another temporary variable. In contrast, you can think of pass-by-reference as passing the actual variable and no temporary copies are made, so that any changes in the variable is 'saved'. Although there is more to it, it might be easier way to thinking about it
user814628
Related Q & A:
- How to pass javascript jQuery variable value in php array?Best solution by Stack Overflow
- How to pass ajax value to controller in codeigniter?Best solution by Stack Overflow
- What is a parameterized reference in Java?Best solution by kodejava.org
- What is the default value for HashMap in Java?Best solution by Stack Overflow
- How to pass the value from the javascript to servlet?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.