Java argument passing [duplicate] - java

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.

Related

If I pass an object to a constructor and then modify that object, will it modify the original? Why? [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 2 years ago.
If I have a class that looks like this:
class Web {
WebDriver driver;
public Web(WebDriver driver) {
this.driver = driver;
}
}
will this.driver used throughout the Web class affect the original driver that was passed in? I need them to both reference the same object.
Yes, in Java any object that is not a primitive is passed as a reference. So if you want to have control on the object inside this class or from wherever you called, it is possible. The drawback is that you cannot assume an object will never be changed in you class, unless you create a copy of it (for example with a clone() )
Yes it will. When a method performs an operation using the reference in this.driver, it will be performing an operation on the same WebDriver that you passed to the constructor. If that method modifies the state of the object ... it is the object that you passed in that is being modified.
Java call semantics are "call by value" aka "pass by value"
When you pass an object, the value you are passing is the reference to the object.
Java doesn't make copies of objects implicitly.
In your example code in the question, you are not actually modifying the object that you passed to the constructor. (But you certainly could ...)
If you don't what the constructor (or some other method of the class) to be able to modify the object passed to it, you could:
pass it an immutable object, or
pass it a copy (or clone) of the original object.

Can we define this keyword as a reference variable in Java? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class Temp {
private int x = 3;
public void show() {
this.x = 4;
this.show(); // same as show();
}
}
Can we say that this is a reference variable ?
From the Java Language Specification:
[...] the keyword this denotes a value that is a reference to the object for which the instance method or default method was invoked [...]
So this is not a reference variable, it is in fact a keyword. From the above description you could say it behaves like a reference variable (or better said a constant since you can't change it) if it is used in a certain context.
this is a reference indeed, however it is a constant, thus you can't change its value. this always references an object that object method was invoked on.
Yes. it is in fact a reference variable which refers to the current object.

Java Passing an Object as Argument in Methods [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 9 years ago.
I've gone through a few answers regarding Java always passing by value for example in the below code.
public class Sample {
public void show(String s){
s="A";
}
public static void main(String []args) {
String s="B";
new Sample().show(s);
System.out.println(s);
}
}
String s; Means just a reference is created as my teacher put it no object is created
so, in the above code s=B; means s holds the reference of B i.e. the place in memory where it is stored is my understanding of this correct?
When do show(s); In the definition of the method show no new String object is created, just the reference is passed.
The String s created in main method and the one in show method both refer to the same object in memory ,if both refer to the same object should "A" not be printed to screen?
Both refer to same object so why does "A " not get printed and "B" gets printed?
I'm a newbie in Java I have read a few previous answers regarding the issue and an answer with a diagram with 2 reference arrows pointing to same object confuses me even more
I may have misunderstood the answer so please don`t close my question as its been repeated I need some help with this.
It is passed by reference, but the reference itself is passed by value, and in show you override the reference.
And that's why you should teach pointers :/

Does a List object get passed by reference? [duplicate]

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.

in Java the arguments are passed by value? [duplicate]

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.

Categories