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 |
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 中由于存放的內(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 |
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 |
×
|
在沒有執(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 |
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!”));
因此,你只要按上面的方法去分析,就會發(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 ;后:
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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