亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Java 方法中參數(shù)傳遞

系統(tǒng) 2210 0

Java 方法中參數(shù)傳遞 【網(wǎng)上摘錄】


一個容易忽略的問題,需要注意的地方 。在網(wǎng)上,眾說紛紜。找到一個跟自己理解很近的,貼出來。值得引起注意。

摘錄: Java 中的參數(shù)傳遞機制一直以來大家都爭論不休,究竟是“傳值”還是“傳址(傳引用)”,爭論的雙方各執(zhí)一詞,互不相讓。不但“菜鳥”們一頭霧水,一些“老鳥”也只知道結(jié)果卻說不出所以然來。我相信看過下面的內(nèi)容后,你就會明白一些。

先看 基本類型作為參數(shù)傳遞 的例子:

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;

??? }

}

我想這個例子大家都明白, 基本類型作為參數(shù)傳遞時,是傳遞值的拷貝,無論你怎么改變這個拷貝,原值是不會改變的 ,輸出的結(jié)果證明了這一點:

Before change, n = 3

After changeData(n), n = 3

?

那么,我們現(xiàn)在來看看 對象作為參數(shù)傳遞 的例子,這也是大家爭論的地方。

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!");

??? }

}

先看輸出結(jié)果:

Before change, sb = Hello

After changeData(n), sb = Hello World!

從結(jié)果來看, sb 的值被改變了,那么是不是可以說:對象作為參數(shù)傳遞時,是把對象的引用傳遞過去,如果引用在方法內(nèi)被改變了,那么原對象也跟著改變。從上面例子的輸出結(jié)果來看,這樣解釋是合理。

現(xiàn)在我們對上面的例子稍加改動一下:

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!");

??? }

}

按照上面例子的經(jīng)驗:對象作為參數(shù)傳遞時,是把對象的引用傳遞過去,如果引用在方法內(nèi)被改變了,那么原對象也跟著改變。你會認為應(yīng)該輸出:

Before change, sb = Hello

After changeData(n), sb = Hi World!

但運行一下這個程序,你會發(fā)現(xiàn)結(jié)果是這樣的:

Before change, sb = Hello

After changeData(n), sb = Hello

這就是讓人迷惑的地方,對象作為參數(shù)傳遞時,同樣是在方法內(nèi)改變了對象的值,為什么有的是改變了原對象的值,而有的并沒有改變原對象的值呢?這時候究竟是“傳值”還是“傳引用”呢?

下面就讓我們仔細分析一下,來揭開這中間的奧秘吧。

先看 Test2 這個程序:

StringBuffer sb = new StringBuffer("Hello ");

這一句執(zhí)行完后,就會在內(nèi)存的堆里生成一個 sb 對象,請看圖1:

?

?

如圖1所示, sb 是一個引用,里面存放的是一個地址 @3a ”(這個 @3a ”是我舉的代表內(nèi)存地址的例子,你只需知道是個內(nèi)存地址就行了),而這個地址正是“ Hello ”這個字符串在內(nèi)存中的地址。

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> 執(zhí)行這一句后,就把 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>

<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>
此時, sb strBuf 中由于存放的內(nèi)存地址相同,因此都指向了“ Hello ”。

strBuf.append("World!");

執(zhí)行 changeData 方法中的這一句后,改變了 strBuf 指向的內(nèi)存中的值,如下圖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>
所以, 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>
在沒有執(zhí)行到 changeData 方法的 strBuf = new StringBuffer(“Hi “); 之前,對象在內(nèi)存中的圖和上例中“圖 2 ”是一樣的,而執(zhí)行了 strBuf = new StringBuffer(“Hi “); 之后,則變成了:

此時, strBuf 中存放的不再是指向“ Hello ”的地址,而是指向“ Hi ”的地址“ @3b (同樣“ @3b” 是個例子)了, new 操作符操作成功后總會在內(nèi)存中新開辟一塊存儲區(qū)域。

strBuf.append("World!");

?????? 而執(zhí)行完這句后,

<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> 通過上圖可以看到,由于 sb strBuf 中存放地址不一樣了,所以雖然 strBuf 指向的內(nèi)存中的值改變了,但 sb 指向的內(nèi)存中值并不會變,因此也就輸出了下面的結(jié)果:

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!”));

因此,你只要按上面的方法去分析,就會發(fā)現(xiàn) String 對象和基本類型一樣,一般情況下作為參數(shù)傳遞,在方法內(nèi)改變了值,而原對象是不會被改變的。

?

綜上所述,我們就會明白, Java 中對象作為參數(shù)傳遞時,是把對象在內(nèi)存中的地址拷貝了一份傳給了參數(shù)。

你可以試著按上面的畫圖法分析一下下面例子的結(jié)果,看看運行結(jié)果與你分析的結(jié)果是否一樣:

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!");

??? }

}

?

??? 提示:

???????? 執(zhí)行完 strBuf = sb2 ;后:

Java 方法中參數(shù)傳遞


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产在线一区二区三区欧美 | 久久免费观看视频 | 亚洲精品一区二区三区在 | 在线播放精品一区二区啪视频 | 韩国精品一区 | 国产欧美一区二区三区视频 | 久久免费资源福利资源站 | 欧美午夜不卡 | 久草精品免费 | 日韩精品中文字幕一区二区三区 | 国产女主播喷出白浆视频 | 欧美日韩视频一区二区 | 毛片a级放荡的护士hd | 在线视频欧美日韩 | 日日摸日日 | 婷婷中文字幕 | 国产精品亚洲综合第一区 | 日韩中文字幕久久精品 | 精品69久久久久久99 | 久久中文字幕久久久久 | 91视频网址| 四虎影视免费在线观看 | 男女性高清爱潮视频免费观看 | 成人免费毛片一区二区三区 | 欧美精品亚洲二区 | 久久的爱久久久久的快乐 | 欧美一级www毛片 | 国产精品久久久久桃色tv | 中文字幕在线看视频一区二区三区 | 国产中文字幕在线免费观看 | 日韩成a人片在线观看日本 日韩成人 | 国产精品在线观看 | 日本永久视频 | 波多野结衣一区二区三区在线观看 | 成人一级片在线观看 | 久操免费在线视频 | 福利网站在线播放 | 国产99视频在线观看 | 中文字幕一区久久久久 | 四虎影院地址 | 精品中文字幕乱码一区二区 |