Java 方法中参数传递

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

?

如图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

<shape id="_x0000_s1029" style="left: 6727px; width: 1252px; position: absolute; top: 10695px; height: 544px;" type="#_x0000_t202" fillcolor="silver"><textbox></textbox></shape>

Hello

<stroke endarrow="block"></stroke><shape id="_x0000_s1031" style="left: 4380px; width: 469px; position: absolute; top: 10559px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox></textbox></shape>

sb

<shape id="_x0000_s1032" style="left: 5554px; width: 781px; position: absolute; top: 11463px; height: 408px;" stroked="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

1

<wrap type="topAndBottom"></wrap> 执行这一句后,就把 sb 传给了 changeData 方法中的 StringBuffer strBuf ,由于 sb 中存放的是地址,所以, strBuf 中也将存放相同的地址,请看图2:

<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

<shape id="_x0000_s1049" style="left: 6727px; width: 1252px; position: absolute; top: 10695px; height: 544px;" type="#_x0000_t202" fillcolor="silver"><textbox></textbox></shape>

Hello

<stroke endarrow="block"></stroke><shape id="_x0000_s1051" style="left: 4380px; width: 469px; position: absolute; top: 10559px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox></textbox></shape>

sb

<shape id="_x0000_s1052" style="left: 5554px; width: 781px; position: absolute; top: 11463px; height: 408px;" stroked="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

1

<wrap type="topAndBottom"></wrap><group id="_x0000_s1053" 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_s1054" 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_s1055" style="left: 4223px; width: 783px; position: absolute; top: 10831px; height: 408px;" type="#_x0000_t202"><textbox></textbox></shape></group>

@3a

<shape id="_x0000_s1056" style="left: 6727px; width: 1252px; position: absolute; top: 10695px; height: 544px;" type="#_x0000_t202" fillcolor="silver"><textbox></textbox></shape>

Hello

<stroke endarrow="block"></stroke><shape id="_x0000_s1058" style="left: 4380px; width: 469px; position: absolute; top: 10559px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox></textbox></shape>

sb

<shape id="_x0000_s1059" style="left: 5554px; width: 781px; position: absolute; top: 11463px; height: 408px;" stroked="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

1

<wrap type="topAndBottom"></wrap> 图2

<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

<shape id="_x0000_s1029" style="left: 6727px; width: 1252px; position: absolute; top: 10695px; height: 544px;" type="#_x0000_t202" fillcolor="silver"><textbox></textbox></shape>

Hello

<stroke endarrow="block"></stroke><shape id="_x0000_s1031" style="left: 4380px; width: 469px; position: absolute; top: 10559px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox></textbox></shape>

sb

<shape id="_x0000_s1032" style="left: 4250px; width: 783px; position: absolute; top: 11854px; height: 407px;" type="#_x0000_t202"><textbox></textbox></shape>

@3a

<stroke endarrow="block"></stroke><shape id="_x0000_s1034" style="left: 4251px; width: 977px; position: absolute; top: 11529px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox></textbox></shape>

strBuf

<shape id="_x0000_s1035" style="left: 5541px; width: 781px; position: absolute; top: 12238px; height: 407px;" stroked="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

2

<wrap type="topAndBottom" anchory="page"></wrap>
此时, sbstrBuf 中由于存放的内存地址相同,因此都指向了“ 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

<shape id="_x0000_s1029" style="left: 6727px; width: 1252px; position: absolute; top: 10695px; height: 544px;" type="#_x0000_t202" fillcolor="silver"><textbox style=""></textbox></shape>

Hello World!

<stroke endarrow="block"></stroke><shape id="_x0000_s1031" style="left: 4380px; width: 469px; position: absolute; top: 10559px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

sb

<shape id="_x0000_s1032" style="left: 4250px; width: 783px; position: absolute; top: 11854px; height: 407px;" type="#_x0000_t202"><textbox style=""></textbox></shape>

@3a

<stroke endarrow="block"></stroke><shape id="_x0000_s1034" style="left: 4251px; width: 977px; position: absolute; top: 11529px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

strBuf

<shape id="_x0000_s1035" style="left: 5642px; width: 781px; position: absolute; top: 12256px; height: 408px;" stroked="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

3

<wrap type="topAndBottom" anchory="page"></wrap> 图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

<shape id="_x0000_s1039" style="left: 6727px; width: 1252px; position: absolute; top: 10695px; height: 544px;" type="#_x0000_t202" fillcolor="silver"><textbox style=""></textbox></shape>

Hello

<stroke endarrow="block"></stroke><shape id="_x0000_s1041" style="left: 4380px; width: 469px; position: absolute; top: 10559px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

sb

<shape id="_x0000_s1042" style="left: 4250px; width: 783px; position: absolute; top: 11854px; height: 407px;" type="#_x0000_t202"><textbox style=""></textbox></shape>

@3b

<stroke endarrow="block"></stroke><shape id="_x0000_s1044" style="left: 4251px; width: 977px; position: absolute; top: 11529px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

strBuf

<shape id="_x0000_s1045" style="left: 5541px; width: 781px; position: absolute; top: 12238px; height: 407px;" stroked="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

4

<shape id="_x0000_s1046" style="left: 6738px; width: 1252px; position: absolute; top: 11713px; height: 543px;" type="#_x0000_t202" fillcolor="silver"><textbox style=""></textbox></shape>

Hi

<shape id="_x0000_s1047" style="left: 5642px; width: 782px; position: absolute; top: 11169px; height: 544px;" stroked="f" filled="f" type="#_x0000_t202"><textbox></textbox></shape>

×

<stroke endarrow="block"></stroke><wrap type="topAndBottom" anchory="page"></wrap>
在没有执行到 changeData 方法的 strBuf = new StringBuffer(“Hi “); 之前,对象在内存中的图和上例中“图 2 ”是一样的,而执行了 strBuf = new StringBuffer(“Hi “); 之后,则变成了:

图4

此时,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

<shape id="_x0000_s1029" style="left: 6727px; width: 1252px; position: absolute; top: 10695px; height: 544px;" type="#_x0000_t202" fillcolor="silver"><textbox style=""></textbox></shape>

Hello

<stroke endarrow="block"></stroke><shape id="_x0000_s1031" style="left: 4380px; width: 469px; position: absolute; top: 10559px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

sb

<shape id="_x0000_s1032" style="left: 4250px; width: 783px; position: absolute; top: 11854px; height: 407px;" type="#_x0000_t202"><textbox style=""></textbox></shape>

@3b

<shape id="_x0000_s1033" style="left: 4251px; width: 977px; position: absolute; top: 11529px; height: 408px;" stroked="f" filled="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

strBuf

<shape id="_x0000_s1034" style="left: 5541px; width: 781px; position: absolute; top: 12238px; height: 407px;" stroked="f" type="#_x0000_t202"><textbox style=""></textbox></shape>

5

<shape id="_x0000_s1035" style="left: 6738px; width: 1252px; position: absolute; top: 11713px; height: 543px;" type="#_x0000_t202" fillcolor="silver"><textbox style=""></textbox></shape>

Hi World!

<stroke endarrow="block"></stroke><wrap type="topAndBottom" anchory="page"></wrap> 通过上图可以看到,由于 sbstrBuf 中存放地址不一样了,所以虽然 strBuf 指向的内存中的值改变了,但 sb 指向的内存中值并不会变,因此也就输出了下面的结果:

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;后:

图6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值