java 函数参数传递

java 函数参数传递
java 函数参数传递都是传值
对于基本数据类型,传递的是数据的拷贝;
对于引用类型,传递的是引用的拷贝; 

  int x = 3;
  int y = 4;
  change(x, y);
  System.out.println("x=" + x + "y=" + y);
  //不可交换

  int[] num1 = new int[]{3,4};
  change(num1);
  System.out.println("num x=" + num1[0] + "num y=" + num1[1]);
  //可交换

  Point pt = new Point();
  pt.x = 3;
  pt.y = 4;
  change(pt);
  System.out.println("pt.x=" + pt.x + "pt.y=" + pt.y);
//可交换

public static void change(int x, int y) {
  x = x + y;
  y = x - y;
  x = x - y;
 }

 public static void change(int[] num) {
  num[0] = num[0] + num[1];
  num[1] = num[0] - num[1];
  num[0] = num[0] - num[1];
 }

 public static void change(Point pt) {
  pt.x = pt.x + pt.y;
  pt.y = pt.x - pt.y;
  pt.x = pt.x - pt.y;
 }


你可能感兴趣的:(java 函数参数传递)