1

Here is my code to swap two numbers using wrapper class, i am aware of the fact that java only has pass by value ,so we cannot use use something like a pointer to pass the address of variables.For this i created objects for wrapper class Integer a,b.
But this code doesn't work , the comments in the code section explain my approach , Can someone please tell me where did i go wrong.

class swp{

public static void main(String[] args) {
Integer x = new Integer(5);  //x --> obj with 5 int value
Integer y = new Integer (6); //y --> obj with 6 int value


System.out.println("x = "+ x+ "   " +"y = " + y);
swap(x,y);
System.out.println("x = " + x+ "   " +"y = " + y);
}


//the values in x and y are copied in a and b 



static  void swap(Integer a,Integer b){         //a ,x--> obj with 5 int value .b,y --> obj with 6 int value
        int temp = a.intValue();              // temp contains 5
        a = b.intValue() ;                   // value at the obj ref. by a has changed to 6
        b = temp;                          //value at the obj ref. by a has changed to 5


        System.out.println("in func :  "+"a = " + a+ "   " +"b = " + b);       
}

}

output

 a = 5   b = 6
 in func :  a = 6   b = 5
 a = 5   b = 6

I know i could do this using the following approach

void swap(class_name obj1,class_name obj2){
       int temp = obj1.x;
       obj1.x =obj2.x;
       obj2.x = temp;
}

But i want to know what exactly is wrong with my approach.

  • 1
    Java passes **references by value**. `Integer` is **immutable**. So no, you cannot do what you want. Although note two things `new Integer` should never be called, and values of `Integer` `[-128, 128)` are cached so `Integer x = 5` will always be the same instance of `Integer`. – Boris the Spider Dec 16 '18 at 19:45
  • You're just moving the local pointers around. The `swap` function doesn't actually do anything. And (to my knowledge) the `java.lang.Integer` class is *immutable*, so what you're asking isn't possible unless you write your own wrapper. Why do you want to do this? – Silvio Mayolo Dec 16 '18 at 19:46
  • Ok , i got the point it has to do with the imutability of the wrapper class Integer itself . So help me understand this and plz tell me if i am wrong .In 'main' 'x' and 'y' still point to the same location , however inside the 'swap' function , where i thought that the value at the location pointed by 'a' and 'b' has changed instead 'a' and 'b' now point to an entirely different location which has value same as 'b.intvalue()' and 'a.intvalue' – user8157045 Dec 16 '18 at 20:08

1 Answers1

2

Not using Integer directly, but you can using an Integer (or int) array. Like,

public static void main(String[] args) {
    int[] arr = { 5, 6 };
    System.out.println("a = " + arr[0] + "   " + "b = " + arr[1]);
    swap(arr);
    System.out.println("a = " + arr[0] + "   " + "b = " + arr[1]);
}

private static void swap(int[] arr) {
    int t = arr[0];
    arr[0] = arr[1];
    arr[1] = t;
}

Which does output

a = 5   b = 6
a = 6   b = 5

Or create a POJO like,

class MyPair {
    private int a;
    private int b;

    public MyPair(int a, int b) {
        this.a = a;
        this.b = b;
    }

    public String toString() {
        return String.format("a = %d, b = %d", a, b);
    }

    public void swap() {
        int t = a;
        a = b;
        b = t;
    }
}

Then you can do

public static void main(String[] args) {
    MyPair p = new MyPair(5, 6);
    System.out.println(p);
    p.swap();
    System.out.println(p);
}

For the same result.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249