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語言中,若是進行參數傳遞,則默認傳遞的是副本.函數