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()操作之后,改变的是函数外的字段的值.
#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语言中,如果进行参数传递,则默认传递的是副本.