Java:Passing object means passing address, but not for primitive
class objx{
int z;
public objx(int d) {
z=d;
}
}
public class Main {
public void inc(int y)
{
y++;
}
public void incObj(objx ob1)
{
ob1.z++;
}
public static void main(String[] args) {
Main m=new Main();
int j=2;
objx ob=new objx(4);
m.inc(j);
m.incObj(ob);
System.out.println(j+"\n"+ob.z);
}
--------------------------------------------------------------------------
Output
2
5
No comments:
Post a Comment