Friday, December 10, 2010

Accessing Object variable and primitive variable

public static void main(String[] args) {
    test t;
    test s=new test();
    s.y=12;
    t=s;
    s.y++;
    System.out.println(s.y+"\t"+t.y);
    int k,j=12;
    k=j;
    j++;
    System.out.println(k+"\t"+j);

    }
----------------------------------------
OUTPUT
13        13
12        13

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