In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章给大家介绍Java方法传参时采用哪种传递,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
我们来看一个新手甚至写了多年Java的朋友都可能不是十分确定的问题:
在Java方法传参时,究竟是引用传递还是值传递?
为了说明问题, 我给出一个非常简单的class定义:
public class Foo { String attribute; Foo(String s) { this.attribute = s; } void setAttribute(String s) { this.attribute = s; } String getAttribute() { return this.attribute; } }
下面在阐明观点时,可能会多次用到该类。
关于Java里值传递还是引用传递,至少从表现形式上来看,两种观点都有支撑的论据。下面我来一一分析:
观点1:引用传递
理由如下:先看一段代码
public class Main { public static void modifyReference(Foo c){ c.setAttribute("c"); // line DDD } public static void main(String[] args) { Foo fooRef = new Foo("a"); // line AAA modifyReference(fooRef); // line BBB System.out.println(fooRef.getAttribute()); // 输出 c } }
上述示例,输出结果为"c",而不是"c"。
我们在line AAA处新创建了一个Object Foo并将其引用fooRef在line BBB处传给了方法modifyReference()的参数cRef, 该方法内部处理后,fooRef指向的Object中的值从"a"变成了"c", 而引用fooRef还是那个引用, 因此,我们是否可以认为,在line BBB处发生了引用传递?
先留着疑问,我们继续往下看。
观点2:值传递
继续看一段代码
public class Main { public static void changeReference(Foo aRef){ Foo bRef = new Foo("b"); aRef = bRef; // line EEE } public static void main(String[] args) { Foo fooRef = new Foo("a"); // line AAA changeReference(fooRef); // line BBB System.out.println(fooRef.getAttribute()); // 输出 a } }
上述示例,输出结果为"a", 而不是"b"。
我们在line AAA处新创建了一个Object Foo并将其引用fooRef在line EEE处传给了方法changeReference()的参数aRef, 该方法内部引用aRef在line DDD处被重新赋值。如果是引用传递,那么引用aRef在line EEE处已经被指向了新的Object, 输出应该为"b"才对,事实上是怎样的呢?事实上输出了"b",也就是说changeReference()方法改变了传入引用所指对象的值。
观点1和观点2的输出结果多少会让人有些困惑,别急,我们继续往下看。
深入分析
为了详细分析这个问题,把上述两段代码合起来:
public class Main { public static void modifyReference(Foo cRef){ cRef.setAttribute("c"); // line DDD } public static void changeReference(Foo aRef){ Foo bRef = new Foo("b"); // line FFF aRef = bRef; // line EEE } public static void main(String[] args) { Foo fooRef = new Foo("a"); // line AAA changeReference(fooRef); // line BBB System.out.println(fooRef.getAttribute()); // 输出 a modifyReference(fooRef); // line CCC System.out.println(fooRef.getAttribute()); // 输出 c } }
下面来深入内部来详细分析一下引用和Object内部的变化。来看下面图示:
① Line AAA, 申明一个名叫fooRef,类型为Foo的引用,并见其分配给一个新的包含属性值为"f"的对象,该对象类型为Foo。
Foo fooRef = new Foo("a"); // line AAA
Line DDD, method internally, declares a reference named aRef of type Foo, and aRef is initialized to null.
void changeReference(Foo a);
③ CCC Line, changeReference() method is called, reference aRef is assigned to the object pointed to by fooRef.
changeReference(fooRef);
Line FFF, Declares a reference named bRef of type Foo and assigns it to a new object with attribute value "b" of type Foo.
Foo bRef = new Foo("b");
Line EEE, which reassigns the reference aRef to the object containing attribute "b." Note here that fooRef is not reassigned, but aRef.
aRef = bRef;
CCC Line, after calling the method modifyReference(Foo cRef), a new reference cRef is created and assigned to the object containing the attribute "f", which is pointed to by both references fooRef and cRef.
modifyReference(fooRef);
7 Line DDD, cRef.setAttribute("c"); will change the cRef reference to an object with attribute "f" that is also referenced by fooRef.
cRef.setAttribute("c");
The internal attribute value "f" of the object to which the reference fooRef points is also reset to "c."
Java internal method parameters are not passed by reference, but by reference to the "value" of itself, and ultimately by value. Passing an object's reference fooRef to a method's parameter newRef adds a reference to the object, equivalent to an alias. We can access and manipulate the original object through this original reference fooRef, or this is the new reference newRef in the method parameter, and we can also change the value of the reference newRef itself in the parameter, but we cannot change the value of the original reference fooRef.
About Java method when passing parameters to share what kind of pass here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.