Perhatikan contoh berikut:
public class Tukar {
public static void menukar(int a, int b) {
int c;
c=a;a=b;b=c;
}
public static void main(String[] arg) {
int x=9,y=8;
menukar(x,y);
System.out.println("Nilai x = "+x);
System.out.println("Nilai y = "+y);
}
}
Bandingkan dengan contoh berikut:
public class Tukar1 {
int a,b;
public static void menukar(Tukar1 t) {
int c;
c=t.a;t.a=t.b;t.b=c;
}
public static void main(String[] arg) {
Tukar1 tkr=new Tukar1();
tkr.a=9;tkr.b=8;
menukar(tkr);
System.out.println("Nilai a = "+tkr.a);
System.out.println("Nilai b = "+tkr.b);
}
}
No comments:
Post a Comment