Minggu, 18 November 2012


contoh program by Value dan by References public class TestPassByValue { public static void main(String[] args) { // instantiate a TestPassByValue object TestPassByValue t = new TestPassByValue(); int i = 1; // local variable System.out.println("value of i before test = " + i); // pass value of local variable int j = t.test(i); System.out.println("value of i after test = " + i); System.out.println("value of j = " + j); j = t.test2(j); System.out.println("value of i after test2 = " + i); System.out.println("value of j after test2 = " + j); } public int test (int i) { i = ++i; // same as i = i + 1; System.out.println("value of i in test = " + i); return i; } public int test2 (int k) { int i = k; // i refers to instance variable System.out.println("value of k = " + k); i = ++i; System.out.println("value of i in test2 = " + i); return i; } } 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); } }

0 komentar:

Posting Komentar