<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                >[success] # 方法傳參過程 ~~~ public class MaxNum { int a = 0; int b = 0; // 返回 a b 最大值 int max(int a, int b) { int max = a > b ? a : b; return max; } public static void main(String[] args) { MaxNum maxNum = new MaxNum(); int a = 10; int b = 20; int res = maxNum.max(a, b); System.out.println(res); } } ~~~ 上面代碼 執行過程: 1. 為main方法中的變量a、b、res分配空間并初始化。此時 a 初始化為10,初始化為20,res 為空 2. 調用max方法,為`max方法`的`形參`變量`a、b`分配空間。 3. 將`實參變量`的數值賦值到`形參變量`的內存空間中。 4. max方法運行完畢后返回,形參變量空間釋放。 5. main方法中的res變量得到max方法的返回值。 6. main方法結束后釋放相關變量的內存空間。 >[success] # 注意事項 * `基本數據類型`的變量作為`方法的參數傳遞`,`形參變量`數值的`改變`通常`不會影響`到`實參變量的數值`,因為兩個變量有各自獨立的內存空間 ~~~ // 改變形參值并沒有影響實參 public class MaxNum { void max(int a) { // 改變形參的賦值 a = 0; System.out.println("形參a 的值:" + a); } public static void main(String[] args) { int a = 100; MaxNum maxNum = new MaxNum(); // 打印形參值 maxNum.max(a); // 0 System.out.println("實參a 的值:" + a); // 100 } } ~~~ * `引用數據類型`的變量作為方法的參數傳遞時,`形參變量`指向內容的`改變`,`會影響`到`實參變量`指向內容的`數值`,因為兩個變量指向`同一塊內存空間` ~~~ public class MaxNum { void max(int[] ls) { // 改變形參的賦值 ls[0] = 1234; System.out.println("形參a 的值:"+ ls[0] ); // 1234 } public static void main(String[] args) { int[] ls = { 1, 2, 3, 4 }; MaxNum maxNum = new MaxNum(); // 打印形參值 maxNum.max(ls); // 1234 System.out.println("實參a 的值:" + ls[0]); // 1234 } } ~~~ * 當引用數據類型的變量作為方法的參數傳遞時,若形參變量改變指向后再改變指定的內容,則通常不會影響到實參變量指向內容的改變,因為兩個變量指向不同的內存空間。 ~~~ public class MaxNum { void max(int[] ls) { // 相當于給形參的ls 重新賦值已經不在指向之前的實參了 ls = new int[4]; ls[0] = 1234; System.out.println("形參a 的值:"+ ls[0] );// 1234 } public static void main(String[] args) { int[] ls = { 1, 2, 3, 4 }; MaxNum maxNum = new MaxNum(); // 打印形參值 maxNum.max(ls); // 1234 System.out.println("實參a 的值:" + ls[0]); // 1 } } ~~~ >[danger] ##### 總結 1. 基本類型的參數傳輸存儲的數據值。 2. 引用類型的參數傳輸存儲的地址值。 ![](https://img.kancloud.cn/e1/91/e1912f8b057a7501a3bdce495315d95c_1277x589.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看