欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

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

系統(tǒng) 2288 0

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


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

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

先看 基本類型作為參數(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;

??? }

}

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

Before change, n = 3

After changeData(n), n = 3

?

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

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

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

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)驗(yàn):對(duì)象作為參數(shù)傳遞時(shí),是把對(duì)象的引用傳遞過去,如果引用在方法內(nèi)被改變了,那么原對(duì)象也跟著改變。你會(huì)認(rèn)為應(yīng)該輸出:

Before change, sb = Hello

After changeData(n), sb = Hi World!

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

Before change, sb = Hello

After changeData(n), sb = Hello

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

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

先看 Test2 這個(gè)程序:

StringBuffer sb = new StringBuffer("Hello ");

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

?

?

如圖1所示, sb 是一個(gè)引用,里面存放的是一個(gè)地址 @3a ”(這個(gè) @3a ”是我舉的代表內(nèi)存地址的例子,你只需知道是個(gè)內(nèi)存地址就行了),而這個(gè)地址正是“ Hello ”這個(gè)字符串在內(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 中也將存放相同的地址,請(qǐng)看圖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>
此時(shí), 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 這個(gè)程序最后會(huì)輸出:

After changeData(n), sb = Hello World!

?

再看看 Test3 這個(gè)程序。

<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 “); 之前,對(duì)象在內(nèi)存中的圖和上例中“圖 2 ”是一樣的,而執(zhí)行了 strBuf = new StringBuffer(“Hi “); 之后,則變成了:

此時(shí), strBuf 中存放的不再是指向“ Hello ”的地址,而是指向“ Hi ”的地址“ @3b (同樣“ @3b” 是個(gè)例子)了, new 操作符操作成功后總會(huì)在內(nèi)存中新開辟一塊存儲(chǔ)區(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)存中值并不會(huì)變,因此也就輸出了下面的結(jié)果:

After changeData(n), sb = Hello

?

String 類是個(gè)特殊的類,對(duì)它的一些操作符是重載的,如:

String str = “Hello”; 等價(jià)于 String str = new String(“Hello”);

String str = “Hello”;

str = str + “ world!”; 等價(jià)于 str = new String((new StringBuffer(str)).append(“ world!”));

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

?

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

你可以試著按上面的畫圖法分析一下下面例子的結(jié)果,看看運(yùn)行結(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號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 九九热线精品视频6一 | 免费激情网址 | 欧美日韩一区二区综合在线视频 | 日韩在线电影 | 亚洲特黄 | 狠狠操夜夜操 | 成人久久一区 | 精品久久久久久久久久久久 | 一区二区三区日本在线观看 | 亚洲国产精品二区久久 | 欧美天天视频 | 欧美性高清视频免费看www | 亚洲精品久久国产高清 | 亚洲精品午夜一区二区 | 国产成人免费 | 国产一区二区视频在线播放 | 国产免费A片好硬好爽好深小说 | 国产一区二区精品在线观看 | 神马九九 | 欧美一区二区三区爽大粗免费 | 国产综合精品久久亚洲 | 久久一区二区三区免费 | 欧美成人性性 | 最新久久免费视频 | www.309809.com| www亚洲一区 | 欧美a区 | 黄视频网站| 久久第四色 | 色阁阁日韩欧美在线 | 欧美视频区 | 中文字幕视频一区 | 日韩a无吗一区二区三区 | 一区二区高清 | 久久秋霞理论电影 | 久久精品久久久久电影 | 亚洲欧洲日韩国产aa色大片 | 亚洲国产精品网站 | 久久久久欧美精品网站 | 色秀视频免费高清网站 | 久久久视频在线 |