Java 值传递和引用传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20064132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Pass By Value and Pass By Reference
提问by CodeNotFound
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.
此方法useDifferentMap
获取地图并更改值并返回相同的值。修改后的映射将包含修改后的值,并且它的范围对于useDifferentMap
方法来说是本地的。
my question is if java is pass by value is the modified value should not be affected in the original map.
我的问题是如果java是按值传递的,修改后的值不应该在原始地图中受到影响。
so is java pass by value or pass by reference ???
java是按值传递还是按引用传递???
thanks
谢谢
回答by SudoRahul
Java is always pass-by-valueonly, just that in this case, the reference of the HashMap is passed by value. The valueMap
refers to the same object as the inputMap
.
Java 总是只按值传递,只是在这种情况下,HashMap的引用是按值传递的。的valueMap
是指相同的对象inputMap
。
That's why when you add a key-value pair using valueMap
, it is reflected back in inputMap
, as both are referring to the same HashMap object.
这就是为什么当您使用 添加键值对时valueMap
,它会反映在 中inputMap
,因为两者都引用同一个 HashMap 对象。
This answerby Eng.Fouadexplains this concept very nicely.
此答案由Eng.Fouad解释这个概念非常漂亮。
回答by Suresh Atta
Java is pass-by-value.But your doubt is reffering to reference, Even reference in java passed by value.
Java 是按值传递的。但是您的疑问是指引用,甚至是按值传递的 java 中的引用。
So reference value passed, and the map gets effected.
因此传递了参考值,并影响了地图。
You confused with the term pass by value. pass by valuein the sense reference passed as value.
您对“按值传递”一词感到困惑。通过值传递作为值传递的检测基准。
回答by Eng.Fouad
When useDifferentMap(inputMap)
is invoked, inputMap
is assigned to the parameter Map<String, String> valueMap
:
当useDifferentMap(inputMap)
被调用时,inputMap
被分配给参数Map<String, String> valueMap
:
Map<String, String> valueMap = inputMap;
After the assignment, the two references inputMap
and valueMap
now refer to the same object in the memory, and hence modifying that object via one reference, will be reflected to the other reference.
赋值后,两个引用inputMap
和valueMap
现在引用内存中的同一个对象,因此通过一个引用修改该对象,将反映到另一个引用。
回答by jai
Java is pass-byvalue only. No where it is pass-by-reference.
inputMap
and valueMap
(copy of inputMap) are both references to the same hashmap. So, we can access all the methods on the hash map using either of the references - its like two remotes to the same TV.
Java 只是按值传递。没有它是通过引用传递的。
inputMap
和valueMap
(输入映射的副本)都是对同一个哈希映射的引用。因此,我们可以使用任一引用访问哈希映射上的所有方法——就像同一台电视的两个遥控器。
public static Map<String, String> useDifferentMap(Map<String, String> valueMap) {
valueMap=null;
}
try this. If it was pass-by-ref you would have got NPE in the last line of the main method after the call to useDifferentMap()
尝试这个。如果它是通过引用传递的,则在调用 useDifferentMap() 后,您将在 main 方法的最后一行获得 NPE