av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

java語言

java通過值傳遞參數的方法是什么

時間:2025-03-12 14:32:26 java語言 我要投稿
  • 相關推薦

java通過值傳遞參數的方法是什么

  在 Java 應用程序中永遠不會傳遞對象,而只傳遞對象引用。因此是按引用傳遞對象。Java 應用程序按引用傳遞對象這一事實并不意味著 Java 應用程序按引用傳遞參數。以下是小編為大家搜索整理的java通過值傳遞參數的方法是什么,希望能給大家?guī)韼椭?更多精彩內容請及時關注我們應屆畢業(yè)生考試網!

  調用一個方法時候需要提供參數,你必須按照參數列表指定的順序提供。

  例如,下面的方法連續(xù)n次打印一個消息:

  public static void nPrintln(String message, int n) {

  for (int i = 0; i < n; i++)

  System.out.println(message);

  }

  示例

  下面的例子演示按值傳遞的效果。

  該程序創(chuàng)建一個方法,該方法用于交換兩個變量。

  public class TestPassByValue {

  public static void main(String[] args) {

  int num1 = 1;

  int num2 = 2;

  System.out.println("Before swap method, num1 is " +

  num1 + " and num2 is " + num2);

  // 調用swap方法

  swap(num1, num2);

  System.out.println("After swap method, num1 is " +

  num1 + " and num2 is " + num2);

  }

  /** 交換兩個變量的方法 */

  public static void swap(int n1, int n2) {

  System.out.println("\tInside the swap method");

  System.out.println("\t\tBefore swapping n1 is " + n1

  + " n2 is " + n2);

  // 交換 n1 與 n2的值

  int temp = n1;

  n1 = n2;

  n2 = temp;

  System.out.println("\t\tAfter swapping n1 is " + n1

  + " n2 is " + n2);

  }

  }

  以上實例編譯運行結果如下:

  Before swap method, num1 is 1 and num2 is 2

  Inside the swap method

  Before swapping n1 is 1 n2 is 2

  After swapping n1 is 2 n2 is 1

  After swap method, num1 is 1 and num2 is 2

  傳遞兩個參數調用swap方法。有趣的是,方法被調用后,實參的值并沒有改變。

【java通過值傳遞參數的方法是什么】相關文章:

JAVA語言中的參數傳遞11-03

java語言參數傳遞介紹06-12

詳解Java語言中的參數傳遞11-08

java傳值的方法06-24

網頁開發(fā)中JavaScript傳遞參數方法比較08-02

Java方法引用是如何計算值的09-22

Java數組特定值高效判斷方法10-03

java工廠的方法是什么10-31

java垃圾回收的方法是什么07-31