Berikut ini source code dalam bahasa java :
Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main;
/**
*
* @author Barni
* Ahmad Akbar Mariuddin
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int x, y;
Bilangan bil=new Bilangan(10,20);
//inisialisasi x dan y
x=15;
y=30;
System.out.println("Nilai x dan y sebelum passed by value");
System.out.println("Nilai x : "+x);
System.out.println("Nilai y : "+y);
bil.operasi_pass_by_value(x,y); //passed by value
System.out.println("Nilai x dan y setelah passed by value");
System.out.println("Nilai x : "+x);
System.out.println("Nilai y : "+y);
System.out.println("\nNilai bil.a dan bil.b sebelum passed by reference");
bil.tampil();
bil.operasi_pass_by_reference(bil); //passed by reference
System.out.println("Nilai bil.a dan bil.b setelah passed by reference");
bil.tampil();
}
}
Bilangan.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main;
/**
*
* @author Barni
* Ahmad Akbar Mariuddin
*/
public class Bilangan {
private int a ;
private int b ;
public Bilangan(int a, int b)
{
this.a=a;
this.b=b;
}
public void tampil()
{
System.out.println("Nilai bil.a : "+a);
System.out.println("Nilai bil.b : "+b);
}
//passed by value dengan parameter tipe data primitif
public void operasi_pass_by_value(int x, int y)
{
x = x*10;
y = y+15;
}
//passed by value dengan parameter tipe data class
public void operasi_pass_by_reference(Bilangan bil)
{
bil.a = bil.a*10;
bil.b = bil.b+15;
}
}
Sekian dan terimkasih semoga bermanfaat
No comments:
Post a Comment