Primitive types :
Primitive Types VS Object References :
public class PrimitiveTypesVSobjectReferenceMemoAllocationDemo {
public static void main(String[] args) {
System.out.println("CASE OF PRIMITIVE TYPES");
int x = 5 ;
int y = x;
System.out.println(x + " and "+ y +" before");
x = 10;
System.out.println(x + " and "+ y +" after");
y = 20;
System.out.println(x + " and "+ y +" before");
System.out.println("\nCASE OF OBJECT REFERENCES TYPES");
Test a = new Test();
Test b = a;
System.out.println(a.name + " and "+ b.name+ " before");
a.change();
System.out.println(a.name + " and "+ b.name+ " after");
b.change();
System.out.println(a.name + " and "+ b.name+ " after");
}
}
class Test{
String name = "binni";
public void change() {
name = "Bpn";
}
}



No comments:
Post a Comment