This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is Java pass-by-reference?
Does a List object get passed by reference? In other words, if I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically updated when I change it?
in other word: If I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically updated when I change it?
Yes
Does the List object passed by reference?
Value of reference would get passed
public updateList(List<String> names){
//..
}
Explanation
When you call updateList(someOtherList); the value of someOtherList which is a reference will copied to names (another reference in method, bit by bit) so now both of them are referring to same instance in memory and thus it will change
See
Is Java "pass-by-reference" or "pass-by-value"?
Yes, a List that you pass to a method is passed by reference. Any objects you add to the List inside the method will still be in the List after the method returns.
If you add to a list in one method, its original reference in first method will also contain the new item.
java is pass by value, and for objects this means the reference is passed by value.
Yes, because You just pass a reference of ArrayList-Object, to your Object.
Related
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 10 months ago.
What happens when I initialize one object to another in java? Is the reference of that object copied in new object or is a new object created with same member values as in the orginal object;
A obj1=new A("apple");
A obj2=obj1;
What is the correct interpretation for this scenario?
when I initialize one object to another in java
I do not know what you mean by "to another". But you initialized only a single object.
First line
Regarding your first line:
A obj1=new A("apple");
A obj1 declares a reference variable, giving it a name obj1, and specifies that this var will hold a reference to any object of type A. This reference variable is initially null.
Calling new A("apple") constructs an object some place in memory. The result, the return value, of this code is a reference to the new object.
The = assigns that reference to be the content of the variable named obj1.
You can think of a reference variable as containing the address in memory where the beginning of that object's block of allocated memory resides. In Java we never see the literal contents of a reference variable. But we know we can reach the object via that reference variable.
In our daily work of programming in Java, we may generally think of obj1 as being the object. But actually obj1 is a way to find the object, a tether attached to the object, a line we can follow to access the object somewhere else in memory.
Second line
Regarding your second line:
A obj2=obj1;
First you declare a new variable named obj2 to hold a reference to an object of type A. Then you copied the reference from obj1 and put that copy into obj2.
You are left with:
A single object.
Two references to that single object.
It is just the reference coppied, not an actual new object in some other memory space.
Normally for deep-copy you can declare yourself a clone method in that class that uses the properties of the passed object and creates another object with new keyword and returns it. This way you will have a new object when you use clone method. More specifically you can declare your class to implement Cloneable interface and then provide an override implementation for the method clone() which already exists in parent Object class.
For shallow-copy you should again create a clone method in your class and in this method you can just use return super.clone() so that the default clone() method provided by Object class will be used to make a shallow-copy of the object meaning only primitive fields will be actually copied and for any non primitive fields the reference will be copied instead.
For your simple example where the field in this class is only some String you can use the shallow copy of clone already provided by Object class and this will seem enough.
If however you had more non primitive fields in this class, then you will had to override your clone method and provide some implementation so that a deep copy could be returned.
This question already has answers here:
Java: recommended solution for deep cloning/copying an instance
(10 answers)
Closed 8 years ago.
I have a question about using a LinkedList and the .get() operation. Java as I understand passes objects by reference, so if I have a linked list called A, and I do temp B = A.get(i), I retrieve an object B that I can modify and the changes are reflected in A.get(i).
However, if the object B has within it (Say another LinkedList object), I do not get a deep copy correct? Is the solution that I must create a copy constructor for my class 'temp' in this example. Or is there a better, built-in way to do this?
Thanks for your help.
Java as I understand passes objects by reference..
No. Java pass everything by value. If you have a reference type, the reference is passed by value. See this question.
if I have a linked list called A, and I do temp B = A.get(i), I retrieve an object B that I can modify and the changes are reflected in A.get(i).
If you have a list of reference types get(i) will return a reference to a particular instance. The element in the list and your retrieved reference will refer to the same object. So if you change the object in some way, it will be "visible" from both references.
However, if the object B has within it (Say another LinkedList object), I do not get a deep copy correct?
Correct. You get a reference.
Is the solution that I must create a copy constructor for my class 'temp' in this example. Or is there a better, built-in way to do this?
If you need a deep copy of your object, you must implement it yourself.
Java as I understand passes objects by reference
No. It passes references by value. It doesn't pass objects at all [except in the case of RMI.]
so if I have a linked list called A, and I do temp B = A.get(i), I retrieve an object B
No. You retrieve a reference that refers to B. The same B whose reference you passed when you added it to the list.
that I can modify and the changes are reflected in A.get(i).
Yes, see above.
However, if the object B has within it (Say another LinkedList object), I do not get a deep copy correct?
Correct. Just like the first case. No difference whatsoever.
Is the solution that I must create a copy constructor for my class 'temp' in this example.
Solution to what? I've never used a copy constructor or the clone() method in Java since 1997. What problem are you trying to solve?
Or is there a better, built-in way to do this?
To do what?
It does not return a "deep copy" because there is no copying done at all, at least, not at the object level. Let me explain.
When you have an instance of an object, the variable that references that object is a pointer to the object. The object can be referenced and modified by many variables (pointers). Observe the following code:
// Let's assume I have a custom object class called Student
// Here the object is created and s now points to the new Student object
Student s = new Student();
// Here I create another variable that points to the same object
Student s2 = s;
Those two variables both point to the same object and any changes that one variable makes to the object will then be reflected in the other.
This ties into your list example. If you have a LinkedList of objects, it is actually a list of pointers to objects. So calling get(2) on the list will get a reference to the 3rd object in the list. The object that it's referencing is the object, not a copy. So any references, variables, methods etc. that were in this object will still be there.
I hope that answers your question :)
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 8 years ago.
I know there have been several posts explaining argument passing in Java. I also know that the arguments are passed by value but the value is the reference to the object. Say I have the following 2 mthods.
public void append(List<String> list){
list.add("some string");
}
public void incr(int count){
count++;
}
If I call the first method, the object list gets modified and the modified value exists after the method call too. But when I call incr(count), the modified value of count is lost once the method call returns.
Why is it that in some cases, the value gets modified but in the other it does not?
I also know that the arguments are passed by value but the value is the reference to the object.
That is not what java does. Java is pass by value. If something is a reference type, then that reference is passed by value. But that is not the same as being pass by REFERENCE. So, in your second example, the count variable is passed by value, so your changes are lost.
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 9 years ago.
I'm interested to know exactly what's happening under the bonnet when passing a variable or object into a function.
When passing an object or variable into a function, is a new copy of the object/variable created in the new scope? (A set of parentheses constitutes a scope in java right?). Or is the reference to the existing variable/object in memory passed in? Although that would only make sense for a global object/variable?
java is always pass by value so a new variable or reference variable(which refer to some object) will be created in the function to receive the value that has passed to it...
The scope of these variable will be withing that function in which it has created.
One thing you should know that even object are passed by value in java...when people say we pass the object to method ,that time we actually pass the value referred by reference variable not the object...so both the old and new reference variable refer to same object in heap memory..
check this for reference...
http://javadude.com/articles/passbyvalue.htm
http://www.programmerinterview.com/index.php/java-questions/does-java-pass-by-reference-or-by-value/
The easiest way to think of this is to get away from thinking of variables as ever being objects. A reference variable or expression is either null or a pointer to an object of appropriate class for its type.
Under this model, all Java argument passing is by value.
When you pass a reference to a method, you pass the null-or-pointer value to it. Assignment to the argument only affects the argument. It does not affect any variables in the caller's environment. On the other hand, if it is not null it points to the same object as the caller's variable or expression pointed to. Calling a value-changing method in that object changes its value for all code using a pointer to that object, including the caller.
Both - you get a copy of the object reference (for objects), and a copy of the value for primitives.
So unlike C, you can't pass in a variable reference (for a string for example) and end up with it being repointed to something else. And you can't pass in an int, for example, and change it's value within the method - any changes it to it will only be within the method scope.
e.g:
MyObjectHolder holder = new MyObjectHolder();
holder.setObject(new Object());
//holder reference id = 1
// holder.object reference id = 2
doIt(holder);
public void doIt(MyObjectHolder methodScopeHolder) {
// methodScpeHolder reference id = 3
// methodScopeHolder.object reference id = 2
}
In Java your program's "local" variables are maintained in a "stack frame", which is a section of a large array whose elements can contain any data type.
When you call, you copy the parameters (which may be either "scalars" -- chars, ints, floats, etc -- or "references") into a new area of the array (the "top"). Then, during the call, the index values that control which elements of the array you can access are adjusted, and the copied parameters become the "bottom" of a new stack frame, with the called method's local variables being above parameters. So to the new method its copies of the parameters are just like local variables.
Effectively, each method has a "window" into the overall stack, and the "windows" overlap to cover the parameter list.
Of course, when you "pass" an object you're really just passing a reference to the object, and the object itself is not copied.
When you pass a variable, you are passing the reference.
When you pass an object, you are passing a copy of it.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is Java pass by reference?
Hi guys,
I have a question about the arguments passing in Java, I read it from a book "In Java the arguments are always passed by value", what does this mean?
I have no experience of C++ and C, so it is a little bit hard for me to understand it.
Can anyone explain me?
Yes, java method parameters are always passed by value. That means the method gets a copy of the parameter (a copy of the reference in case of reference types), so if the method changes the parameter values, the changes are not visible outside the method.
There are two alternative parameter passing modes:
Pass by reference - the method can basically use the variable just like its caller, and if it assigns a new value of the variable, the caller will see this new value after the method finishes.
Pass by name - the parameter is actually only evaluated when it's accessed inside the method, which has a number of far-reaching consequences.
It means that when you pass a variable to a method, the thing that is passed is the value that is currently held by the variable. Thus, subsequents assignments to the method's argument will not affect the value of that variable (caller side), nor the opposite.
A pass-by-reference means that the callee receives a handle to the caller-side variable. Thus, assignments within the method will affect the caller-side variable.
In Java everything is an object. Object is a pointer like C. But in Java, it points the memory place of a class. Passed by value means, what object's value is, this value is passed by value. For example; Integer a=new Integer(); Integer b=new Integer(); setAInteger(b);
public void setAInteger(Integer c){
a= c;
}
After this operation a points the memory place of b. Lets say, at the beginning a=2500 b=3500, after method is called, new a value is 3500. By the way, 2500 and 3500 are memory addresses.