Java 方法中参数传递 【网上摘录】
一个容易忽略的问题,需要注意的地方 。在网上,众说纷纭。找到一个跟自己理解很近的,贴出来。值得引起注意。
摘录: Java 中的参数传递机制一直以来大家都争论不休,究竟是“传值”还是“传址(传引用)”,争论的双方各执一词,互不相让。不但“菜鸟”们一头雾水,一些“老鸟”也只知道结果却说不出所以然来。我相信看过下面的内容后,你就会明白一些。
先看 基本类型作为参数传递 的例子:
public class Test1 {
?????? public static void main(String[] args) {
??????? int n = 3;
??????? System.out.println("Before change, n = " + n);
??????? changeData(n);
??????? System.out.println("After changeData(n), n = " + n);
??? }
??????
?????? public static void changeData(int nn) {
??????? nn = 10;
??? }
}
我想这个例子大家都明白, 基本类型作为参数传递时,是传递值的拷贝,无论你怎么改变这个拷贝,原值是不会改变的 ,输出的结果证明了这一点:
Before change, n = 3
After changeData(n), n = 3
?
那么,我们现在来看看 对象作为参数传递 的例子,这也是大家争论的地方。
public class Test2 {
?????? public static void main(String[] args) {
??????? StringBuffer sb = new StringBuffer("Hello ");
??????? System.out.println("Before change, sb = " + sb);
????? ?? changeData(sb);
??????? System.out.println("After changeData(n), sb = " + sb);
??? }
??????
?????? public static void changeData(StringBuffer strBuf) {
??????? strBuf.append("World!");
??? }
}
先看输出结果:
Before change, sb = Hello
After changeData(n), sb = Hello World!
从结果来看, sb 的值被改变了,那么是不是可以说:对象作为参数传递时,是把对象的引用传递过去,如果引用在方法内被改变了,那么原对象也跟着改变。从上面例子的输出结果来看,这样解释是合理。
现在我们对上面的例子稍加改动一下:
public class Test3 {
?????? public static void main(String[] args) {
??????? StringBuffer sb = new StringBuffer("Hello ");
??????? System.out.println("Before change, sb = " + sb);
??????? changeData(sb);
??????? System.out.println("After changeData(n), sb = " + sb);
??? }
??????
?????? public static void changeData(StringBuffer strBuf) {
?????? ??? strBuf = new StringBuffer("Hi ");
?????????? strBuf.append("World!");
??? }
}
按照上面例子的经验:对象作为参数传递时,是把对象的引用传递过去,如果引用在方法内被改变了,那么原对象也跟着改变。你会认为应该输出:
Before change, sb = Hello
After changeData(n), sb = Hi World!
但运行一下这个程序,你会发现结果是这样的:
Before change, sb = Hello
After changeData(n), sb = Hello
这就是让人迷惑的地方,对象作为参数传递时,同样是在方法内改变了对象的值,为什么有的是改变了原对象的值,而有的并没有改变原对象的值呢?这时候究竟是“传值”还是“传引用”呢?
下面就让我们仔细分析一下,来揭开这中间的奥秘吧。
先看 Test2 这个程序:
StringBuffer sb = new StringBuffer("Hello ");
这一句执行完后,就会在内存的堆里生成一个 sb 对象,请看图1:
?
?
如图1所示, sb 是一个引用,里面存放的是一个地址 “ @3a ”(这个 “ @3a ”是我举的代表内存地址的例子,你只需知道是个内存地址就行了),而这个地址正是“ Hello ”这个字符串在内存中的地址。
changeData(sb);
<wrapblock></wrapblock><group id="_x0000_s1026" style="margin-top: 46.8pt; z-index: 1; left: 0px; margin-left: 27pt; width: 5in; position: absolute; height: 97.6pt; text-align: left;" coordsize="6260,1700" coordorigin="2825,10218" editas="canvas"><lock aspectratio="t" v:ext="edit"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_s1027" style="left: 2825px; width: 6260px; position: absolute; top: 10218px; height: 1700px;" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></font></shape><shapetype id="_x0000_t202" coordsize="21600,21600" path="m,l,21600r21600,l21600,xe" o:spt="202"><stroke joinstyle="miter"></stroke><path o:connecttype="rect" gradientshapeok="t"></path></shapetype><shape id="_x0000_s1028" style="left: 4223px; width: 783px; position: absolute; top: 10831px; height: 408px;" type="#_x0000_t202"><textbox></textbox></shape></group>
@3a |
Hello |
sb |
图 1 |
<group id="_x0000_s1046" style="margin-top: 0px; z-index: 1; left: 0px; margin-left: 0px; width: 5in; position: absolute; height: 97.6pt; text-align: left;" coordsize="6260,1700" coordorigin="2825,10218" editas="canvas"><font face="宋体, MS Song"><font size="3"><lock aspectratio="t" v:ext="edit"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype></font></font><shape id="_x0000_s1047" style="left: 2825px; width: 6260px; position: absolute; top: 10218px; height: 1700px;" o:preferrelative="f" type="#_x0000_t75"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></shape><shapetype id="_x0000_t202" coordsize="21600,21600" path="m,l,21600r21600,l21600,xe" o:spt="202"><stroke joinstyle="miter"></stroke><path o:connecttype="rect" gradientshapeok="t"></path></shapetype><shape id="_x0000_s1048" style="left: 4223px; width: 783px; position: absolute; top: 10831px; height: 408px;" type="#_x0000_t202"><textbox></textbox></shape></group>
@3a |
Hello |
sb |
图 1 |
@3a |
Hello |
sb |
图 1 |
<wrapblock><group id="_x0000_s1026" style="margin-top: 73.05pt; z-index: 1; left: 0px; margin-left: 23.8pt; width: 5in; position: absolute; height: 150pt; text-align: left;" coordsize="6260,2613" coordorigin="2825,10218" editas="canvas"><lock aspectratio="t" v:ext="edit"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_s1027" style="left: 2825px; width: 6260px; position: absolute; top: 10218px; height: 2613px;" o:preferrelative="f" type="#_x0000_t75"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></shape><shapetype id="_x0000_t202" coordsize="21600,21600" path="m,l,21600r21600,l21600,xe" o:spt="202"><stroke joinstyle="miter"></stroke><path o:connecttype="rect" gradientshapeok="t"></path></shapetype><shape id="_x0000_s1028" style="left: 4223px; width: 783px; position: absolute; top: 10831px; height: 408px;" type="#_x0000_t202"><textbox></textbox></shape></group></wrapblock>
@3a |
Hello |
sb |
@3a |
strBuf |
图 2 |
此时, sb 和 strBuf 中由于存放的内存地址相同,因此都指向了“ Hello ”。
strBuf.append("World!");
执行 changeData 方法中的这一句后,改变了 strBuf 指向的内存中的值,如下图3所示:
?
<wrapblock><group id="_x0000_s1026" style="margin-top: 274.8pt; z-index: 1; left: 0px; margin-left: 18pt; width: 5in; position: absolute; height: 150pt; text-align: left;" coordsize="6260,2613" coordorigin="2825,10218" editas="canvas"><lock aspectratio="t" v:ext="edit"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_s1027" style="left: 2825px; width: 6260px; position: absolute; top: 10218px; height: 2613px;" o:preferrelative="f" type="#_x0000_t75"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></shape><shapetype id="_x0000_t202" coordsize="21600,21600" path="m,l,21600r21600,l21600,xe" o:spt="202"><stroke joinstyle="miter"></stroke><path o:connecttype="rect" gradientshapeok="t"></path></shapetype><shape id="_x0000_s1028" style="left: 4223px; width: 783px; position: absolute; top: 10831px; height: 408px;" type="#_x0000_t202"><textbox style=""></textbox></shape></group></wrapblock>
@3a |
Hello World! |
sb |
@3a |
strBuf |
图 3 |
所以, Test2 这个程序最后会输出:
After changeData(n), sb = Hello World!
?
再看看 Test3 这个程序。
<wrapblock></wrapblock><group id="_x0000_s1036" style="margin-top: 532.2pt; z-index: 2; left: 0px; margin-left: 27pt; width: 5in; position: absolute; height: 150pt; text-align: left;" coordsize="6260,2613" coordorigin="2825,10218" editas="canvas"><lock aspectratio="t" v:ext="edit"></lock><shape id="_x0000_s1037" style="left: 2825px; width: 6260px; position: absolute; top: 10218px; height: 2613px;" o:preferrelative="f" type="#_x0000_t75"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></shape><shape id="_x0000_s1038" style="left: 4223px; width: 783px; position: absolute; top: 10831px; height: 408px;" type="#_x0000_t202"><textbox style=""></textbox></shape></group>
@3a |
Hello |
sb |
@3b |
strBuf |
图 4 |
Hi |
×
|
在没有执行到 changeData 方法的 strBuf = new StringBuffer(“Hi “); 之前,对象在内存中的图和上例中“图 2 ”是一样的,而执行了 strBuf = new StringBuffer(“Hi “); 之后,则变成了:
此时, strBuf 中存放的不再是指向“ Hello ”的地址,而是指向“ Hi ”的地址“ @3b ” (同样“ @3b” 是个例子)了, new 操作符操作成功后总会在内存中新开辟一块存储区域。
strBuf.append("World!");
?????? 而执行完这句后,
<wrapblock><group id="_x0000_s1026" style="margin-top: 1in; z-index: 1; left: 0px; margin-left: 9pt; width: 5in; position: absolute; height: 150pt; text-align: left;" coordsize="6260,2613" coordorigin="2825,10218" editas="canvas"><lock aspectratio="t" v:ext="edit"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_s1027" style="left: 2825px; width: 6260px; position: absolute; top: 10218px; height: 2613px;" o:preferrelative="f" type="#_x0000_t75"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></shape><shapetype id="_x0000_t202" coordsize="21600,21600" path="m,l,21600r21600,l21600,xe" o:spt="202"><stroke joinstyle="miter"></stroke><path o:connecttype="rect" gradientshapeok="t"></path></shapetype><shape id="_x0000_s1028" style="left: 4223px; width: 783px; position: absolute; top: 10831px; height: 408px;" type="#_x0000_t202"><textbox style=""></textbox></shape></group></wrapblock>
@3a |
Hello |
sb |
@3b |
strBuf |
图 5 |
Hi World! |
After changeData(n), sb = Hello
?
String 类是个特殊的类,对它的一些操作符是重载的,如:
String str = “Hello”; 等价于 String str = new String(“Hello”);
String str = “Hello”;
str = str + “ world!”; 等价于 str = new String((new StringBuffer(str)).append(“ world!”));
因此,你只要按上面的方法去分析,就会发现 String 对象和基本类型一样,一般情况下作为参数传递,在方法内改变了值,而原对象是不会被改变的。
?
综上所述,我们就会明白, 在 Java 中对象作为参数传递时,是把对象在内存中的地址拷贝了一份传给了参数。
你可以试着按上面的画图法分析一下下面例子的结果,看看运行结果与你分析的结果是否一样:
public class Test4 {
?????? public static void main(String[] args) {
??????? StringBuffer sb = new StringBuffer("Hello ");
??????? System.out.println("Before change, sb = " + sb);
??????? changeData(sb);
??????? System.out.println("After changeData(n), sb = " + sb);
??? }
??????
?????? public static void changeData(StringBuffer strBuf) {
?????? ??? StringBuffer sb2 = new StringBuffer("Hi ");
??????????? strBuf = sb2;
?????????? sb2.append("World!");
??? }
}
?
??? 提示:
???????? 执行完 strBuf = sb2 ;后: