Java基礎(chǔ)學(xué)習(xí)之實(shí)參和形參
關(guān)于變量的賦值:
如果變量是基本數(shù)據(jù)類(lèi)型,此時(shí)賦值的是變量所保存的數(shù)據(jù)值。
如果變量是引用數(shù)據(jù)類(lèi)型,此時(shí)賦值的是變量所保存的數(shù)據(jù)的地址值。
public class ValueTransferTest { public static void main(String[] args) { System.out.println("***********基本數(shù)據(jù)類(lèi)型:****************"); int m = 10; int n = m; System.out.println("m = " + m + ", n = " + n); n = 20; System.out.println("m = " + m + ", n = " + n); System.out.println("***********引用數(shù)據(jù)類(lèi)型:****************"); Order o1 = new Order(); o1.orderId = 1001; Order o2 = o1;//賦值以后,o1和o2的地址值相同,都指向了堆空間中同一個(gè)對(duì)象實(shí)體。 System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId); o2.orderId = 1002; System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId); } } class Order{ int orderId; }
方法的形參的傳遞機(jī)制:值傳遞
1.形參:方法定義時(shí),聲明的小括號(hào)內(nèi)的參數(shù)
實(shí)參:方法調(diào)用時(shí),實(shí)際傳遞給形參的數(shù)據(jù)
2.值傳遞機(jī)制:
如果參數(shù)是基本數(shù)據(jù)類(lèi)型,此時(shí)實(shí)參賦給形參的是實(shí)參真實(shí)存儲(chǔ)的數(shù)據(jù)值。
如果參數(shù)是引用數(shù)據(jù)類(lèi)型,此時(shí)實(shí)參賦給形參的是實(shí)參存儲(chǔ)數(shù)據(jù)的地址值。
public class ValueTransferTest1 { public static void main(String[] args) { int m = 10; int n = 20; System.out.println("m = " + m + ", n = " + n); //交換兩個(gè)變量的值的操作 // int temp = m ; // m = n; // n = temp; ValueTransferTest1 test = new ValueTransferTest1(); test.swap(m, n); System.out.println("m = " + m + ", n = " + n); } public void swap(int m,int n){ int temp = m ; m = n; n = temp; } }
解釋?zhuān)?/p>
public class ValueTransferTest2 { public static void main(String[] args) { Data data = new Data(); data.m = 10; data.n = 20; System.out.println("m = " + data.m + ", n = " + data.n); //交換m和n的值 // int temp = data.m; // data.m = data.n; // data.n = temp; ValueTransferTest2 test = new ValueTransferTest2(); test.swap(data); System.out.println("m = " + data.m + ", n = " + data.n); } public void swap(Data data){ int temp = data.m; data.m = data.n; data.n = temp; } } class Data{ int m; int n; }
到此這篇關(guān)于Java基礎(chǔ)學(xué)習(xí)之實(shí)參形參的文章就介紹到這了,更多相關(guān)Java實(shí)參形參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基于正則表達(dá)式實(shí)現(xiàn)的替換匹配文本功能【經(jīng)典實(shí)例】
這篇文章主要介紹了Java基于正則表達(dá)式實(shí)現(xiàn)的替換匹配文本功能,結(jié)合完整實(shí)例形式分析了java字符串正則替換操作技巧,需要的朋友可以參考下2017-04-04Java GUI圖形界面開(kāi)發(fā)實(shí)現(xiàn)小型計(jì)算器流程詳解
本文章向大家介紹Java GUI圖形界面開(kāi)發(fā)實(shí)現(xiàn)小型計(jì)算器,主要包括布局管理器使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08JAVA對(duì)象和字節(jié)數(shù)組互轉(zhuǎn)操作
這篇文章主要介紹了JAVA對(duì)象和字節(jié)數(shù)組互轉(zhuǎn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Java參數(shù)傳遞及值傳遞實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Java參數(shù)傳遞及值傳遞實(shí)現(xiàn)原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08idea快速找到項(xiàng)目中對(duì)應(yīng)的類(lèi)圖文詳解(包括源碼)
用IDEA開(kāi)發(fā)Java項(xiàng)目時(shí)經(jīng)常會(huì)使用到各種快捷鍵,其中搜索是最常用的之一,下面這篇文章主要給大家介紹了關(guān)于idea如何快速找到項(xiàng)目中對(duì)應(yīng)的類(lèi)(包括源碼)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06IDEA:Error running,Command line is too&n
這篇文章主要介紹了IDEA:Error running,Command line is too long.解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07