-1

I am passing integer object reference

public class Test1 {
    public static void main(String args[]){
        Integer x = new Integer(56);
        Integer y = new Integer(34);
        Test1 test = new Test1();
        test.change(x,y);
        System.out.println("Values inside main() after calling change method");
        System.out.println("x :"+ x +"; y :"+y);
    }
    public void change(Integer x1, Integer y1){

        x1 = 45;
        y1 = 1000;
        System.out.println("Values After Modification inside change method");
        System.out.println("x1 :"+ x1 +"; y1 :"+y1);
    }
}

but there is no change in value of x and y ?? Please help...Thank you in advance. Here is the output.

Values After Modification inside change method
x1 :45; y1 :1000
Values inside main() after calling change method
x :56; y :34
Megamind
  • 41
  • 4

1 Answers1

1

Java is always pass-by-value. This means the references passed to the method are actually copies of the original references.

Unfortunately, they decided to call pointers references, thus confusing newbies. Because those references are passed by value.

Kiran Choudhary
  • 1,125
  • 7
  • 15