java 中函數(shù)的參數(shù)傳遞詳細介紹
java中函數(shù)的參數(shù)傳遞
總結(jié):
1.將對象(對象的引用)作為參數(shù)傳遞時傳遞的是引用(相當于指針)。也就是說函數(shù)內(nèi)對參數(shù)所做的修改會影響原來的對象。
2.當將基本類型或基本類型的包裝集作為參數(shù)傳遞時,傳遞的是值。也就是說函數(shù)內(nèi)對參數(shù)所做的修改不會影響原來的變量。
3.數(shù)組(數(shù)組引用))作為參數(shù)傳遞時傳遞的是引用(相當于指針)。也就是說函數(shù)內(nèi)對參數(shù)所做的修改會影響原來的數(shù)組。
4.String類型(引用)作為參數(shù)傳遞時傳遞的是引用,只是對String做出任何修改時有一個新的String對象會產(chǎn)生,原來的String對象的值不會做任何修改。(但是可以將新的對象的 引用賦給原來的引用,這樣給人的表面現(xiàn)象就是原來的對象變了,其實沒有變,只是原來指向它的引用指向了新的對象)。
package StringTest; class A{ int a=1; char b='A'; public A(){} public A(int _a,char _b){ this.a=_a; this.b=_b; } public String toString(){ return "a="+this.a+",b="+this.b; } } public class ReferenceTest { public static A changeA(A classa){ classa.a=2; classa.b='B'; return classa; } public static String changeString(String str){ System.out.println(str.hashCode()); str=str.toLowerCase(); System.out.println(str.hashCode()); return str; } public static int changeint(int a){ a=a+1; return a; } public static Integer changeInteger(Integer a){ a=new Integer(9); return a; } public static int[] changeintarray(int a[]){ a[0]=10; return a; } public static void printArray(int a[]){ for(int i=0;i<a.length;i++){ System.out.print(a[i]+" "); } System.out.println(); } public static void main(String[] args) { //自定義的對象傳遞的是引用 A a=new A(); A b=changeA(a); System.out.println(a); System.out.println(b); System.out.println("----------------------"); //String對象作為參數(shù)傳遞的也是引用(只是String對象的值不能變,每一個修改String對象的值都會重新創(chuàng)建一個新的String對象用以保存修改后的值,原來的值不會變) String str1="HUHUALIANG"; System.out.println(str1.hashCode()); String str2=changeString(str1); System.out.println(str2.hashCode()); System.out.println(str1); System.out.println(str2); System.out.println("----------------------"); //基本類型是值傳遞 int inta=8; int intb=changeint(inta); System.out.println(inta); System.out.println(intb); System.out.println("----------------------"); //基本類型的包裝集作為參數(shù)傳遞的是值而不是引用 Integer c=new Integer(1); Integer d=changeInteger(c); System.out.println(c); System.out.println(d); System.out.println("----------------------"); //數(shù)組傳遞的是引用 int [] arraya={0,1,2,3}; int [] arrayb=changeintarray(arraya); printArray(arraya); printArray(arrayb); } }
運行結(jié)果:
a=2,b=B
a=2,b=B
----------------------
711139030
711139030
226046678
226046678
HUHUALIANG
huhualiang
----------------------
8
9
----------------------
1
9
----------------------
10 1 2 3
10 1 2 3
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
java將數(shù)據(jù)寫入內(nèi)存,磁盤的方法
下面小編就為大家分享一篇java將數(shù)據(jù)寫入內(nèi)存,磁盤的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01windows定時器配置執(zhí)行java jar文件的方法詳解
這篇文章主要給大家介紹了關(guān)于windows定時器配置執(zhí)行java jar文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11關(guān)于ThreadLocal和InheritableThreadLocal解析
這篇文章主要介紹了關(guān)于ThreadLocal和InheritableThreadLocal解析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03Java對象Serializable接口實現(xiàn)詳解
這篇文章主要介紹了Java對象Serializable接口實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12在MyBatis-Plus中關(guān)閉SQL日志輸出的方法
在使用MyBatis-Plus開發(fā)Java應(yīng)用程序時,隨著項目的復(fù)雜性提升,SQL日志的輸出可能會導(dǎo)致日志文件變得龐大,影響系統(tǒng)的性能和可維護性,本文將介紹如何在Spring Boot項目中關(guān)閉MyBatis-Plus的SQL日志輸出,感興趣的朋友一起看看吧2025-04-04