方法调用中的别名问题

class Letter {
	char c;
}

public class PassObject {
	static void f(Letter y) {
		y.c = 'z';
		System.out.println(y.c);
	}
	
	public static void main(String[] args) {
		Letter x = new Letter();
		x.c = 'a';
		System.out.println(x.c);
		f(x);
		System.out.println(x.c);
	}
}

在调用f()的时候,传递的是Letter对象的引用,而不是Letter对象的副本,所以在f()操做以后,改变的是函数外的字段的值.java

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void f(char y)
{
  y = 'z';
}

void ff(char *y)
{
  *y = 'z';
}

int main(void)
{
  char x = 'x';
  printf("x is: %c(before pseudo change)\n", x);
  f(x);
  printf("x is: %c(after pseudo change)\n", x);
  ff(&x);
  printf("x is: %c(after veritable change)\n", x);

  return 0;
}

在C语言中,若是进行参数传递,则默认传递的是副本.函数

相关文章
相关标签/搜索