i have done the following sample with to check my knowledge
import java.util.Map;
public class HashMap {
public static Map<String, String> useDifferentMap(Map<String, String> valueMap) {
valueMap.put("lastName", "yyyy");
return valueMap;
}
public static void main(String[] args) {
Map<String, String> inputMap = new java.util.HashMap<String, String>();
inputMap.put("firstName", "xxxx");
inputMap.put("initial", "S");
System.out.println("inputMap : 1 " + inputMap);
System.out.println("changeMe : " + useDifferentMap(inputMap));
System.out.println("inputMap : 2 " + inputMap);
}
}
the output is :
original Map : 1 {initial=S, firstName=xxxx}
useDifferentMap : {lastName=yyyy, initial=S, firstName=xxxx}
original Map : 2 {lastName=yyyy, initial=S, firstName=xxxx}
this method useDifferentMap
gets the map and changes the value and returns back the same.
the modified map will contain the modified value and the scope of it is local for the useDifferentMap
method.
my question is if java is pass by value is the modified value should not be affected in the original map.
so is java pass by value or pass by reference ???
thanks
Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that's why it's called pass by value. Pass by Reference: An alias or reference to the actual parameter is passed to the method, that's why it's called pass by reference.
Pass by Value: It is a process in which the function parameter values are copied to another variable and instead this object copied is passed. This is known as call by Value. Pass by Reference: It is a process in which the actual copy of reference is passed to the function. This is called by Reference.
"Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9. "Passing by reference" means that you pass the variable itself into the function (not just the value). So, in your example, it would pass an integer object with the value of 9.
Pass-by-value means that the value of a variable is passed to a function/method. Pass-by-reference means that a reference to that variable is passed to the function. The latter gives the function a way to change the contents of the variable. By those definitions, Java is always pass-by-value.
Java always uses the pass-by-value concept to pass the arguments. In the scenario mentioned, the reference to the HashMap itself is passed by value. The valueMap
refers to the same object as the inputMap
since both of them are referring to the same object.
That's why when you add a key-value pair using valueMap
, it is reflected back in inputMap
.
Checkout out this simple, yet nicely written answer by Eng.Fouad for a picturized version of the concept. Feel free to read a few more answers in that same question that has more in-depth information.
Java is pass-by-value. But your doubt is reffering to reference, Even reference in java passed by value.
So reference value passed, and the map gets effected.
You confused with the term pass by value. pass by value in the sense reference passed as value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With